Skip to content

Instantly share code, notes, and snippets.

View rish-16's full-sized avatar
🤖
backpropagating through memories

Rishabh Anand rish-16

🤖
backpropagating through memories
View GitHub Profile
@rish-16
rish-16 / simulation.py
Created May 11, 2019 15:34
Quantum circuit with Qiskit
import qiskit as qk
# defining our Qubits and Bits
q = qk.QuantumRegister(2)
c = qk.ClassicalRegister(2)
# Creating a circuit with our Qubits and Bits
circuit = qk.QuantumCircuit(q, c)
# Adding Gates
@rish-16
rish-16 / instantiation.py
Created May 11, 2019 16:42
Creating Qubits and Classical Bits in Qiskit
import qiskit as qk
# Creating Qubits
q = qk.QuantumRegister(2)
# Creating Classical Bits
c = qk.ClassicalRegister(2)
@rish-16
rish-16 / circuit.py
Created May 11, 2019 16:44
Building a Quantum Circuit with our Qubits and Classical Bits.
circuit = qk.QuantumCircuit(q, c)
@rish-16
rish-16 / gates.py
Created May 11, 2019 16:46
Adding gates into our Quantum Circuit
# Hadamard Gate on the first Qubit
circuit.h(q[0])
# CNOT Gate on the first and second Qubits
circuit.cx(q[0], q[1])
# Measuring the Qubits
circuit.measure(q, c)
@rish-16
rish-16 / visualize.py
Created May 11, 2019 16:48
Visualizing the Quantum Circuit
print (circuit)
# Using Qiskit Aer's Qasm Simulator
simulator = qk.BasicAer.get_backend('qasm_simulator')
# Simulating the circuit using the simulator to get the result
job = qk.execute(circuit, simulator)
result = job.result()
# Getting the aggregated binary outcomes of the circuit.
counts = result.get_counts(circuit)
print (counts)
@rish-16
rish-16 / connect.py
Created May 13, 2019 06:59
Connecting to the IBM Q 4 Qubit Machine
import qiskit import qk
'''
When you run the script once, you do not need to add
your API key again as your account has already been
saved in memory
'''
qk.IBMQ.save_account('YOUR_API_KEY')
qk.IBMQ.load_accounts()
'''
Applying a Hadamard Gate on the n Qubits
to get a final bitstring of size n
The bitstring will be converted to a
decimal number (integer) between 0 and 2^3 - 1 (7)
'''
for i in range(n):
circ.h(q[i])
circ.measure(q,c)
'''
`n` is the number of Qubits needed to
generate a a random number between
0 and 2^n - 1
'''
n = 3
'''
Creating a Quantum Register with `n` Qubits and
`n` Classical Bits where n=3
# The backend simulator available to me
backend = qk.IBMQ.get_backend('ibmqx4')