from qiskit import QuantumCircuit
Qiskit is an open source software development kit (SDK) for working with quantum computation. There are several excellent tutorials for getting started with qiskit, as well as loads of great documentation you can find at qiskit.org. The following walks through some of the very basics including how to create quantum circuits, simulate measurments, and work with state vectors.
Creating quantum circuits
First, we create a 3-qubit quantum circuit:
= QuantumCircuit(3) qc
Next, we add some gates and draw the resulting circuit:
0)
qc.x(2)
qc.h(0,2)
qc.cx(1,2)
qc.cx(='mpl') qc.draw(output
Measurements
We can simulate a measurement, but first we need to import a simulator:
from qiskit.providers.aer import QasmSimulator
= QasmSimulator() simulator
Measure using a classical register explicitly
One way to measure is to use a circuit having both a quantum and classical register:
= QuantumCircuit(3, 3) circuit
Now, let’s add some gates including some measuments:
0)
circuit.h(0, 1)
circuit.cx(0, 2)
circuit.cx(0,1,2], [0,1,2])
circuit.measure([='mpl') circuit.draw(output
Next, we simulate and count the results:
= simulator.run(circuit, shots=1000)
job = job.result()
result = result.get_counts(circuit)
counts print(counts)
{'000': 469, '111': 531}
We can plot the results using qiskits built in visualization:
from qiskit.visualization import plot_histogram
plot_histogram(counts)
Using the measure_all()
method
Alternatively, if we want to measure all the qubits we can use the measure_all()
method:
= QuantumCircuit(3) circuit
Now, let’s add some gates including some measuments:
0)
circuit.h(0, 1)
circuit.cx(0, 2)
circuit.cx(
circuit.measure_all()='mpl') circuit.draw(output
We should see similar results:
= simulator.run(circuit, shots=1000)
job = job.result()
result = result.get_counts(circuit)
counts print(counts)
{'000': 490, '111': 510}
Of course, this will only work if we want to measure all the qubits in the output state of the circuit.
Circuit orientation
Qiskit orients circuits so that the qubits \(q_0, q_1,\ldots, q_{n-1}\) labelling the circut from top to bottom correspond to the input state \(|q_{n-1}\cdots q_1q_0\rangle\). To see this, let’s create a simple gate that maps \(|000\rangle\mapsto|100\rangle\).
= QuantumCircuit(3)
circ 2)
circ.x(
circ.measure_all()='mpl') circ.draw(output
To see that the circuit above does the trick, let’s measure:
= simulator.run(circ, shots=1000)
job = job.result()
result = result.get_counts(circ)
counts print(counts)
{'100': 1000}
I prefer the flipped orientation where the top qubit in the circuit corresponds to the left qubit in the tensor product. To have qiskit draw circuits with my prefered orientation, we simply pass reverse_bits=True
to the draw()
method:
='mpl', reverse_bits=True) circ.draw(output
= simulator.run(circ, shots=1000)
job = job.result()
result = result.get_counts(circ)
counts print(counts)
{'100': 1000}
State vectors and Bloch spheres
We can use qiskits Statevector
object work with \(n\)-qubit states.
from qiskit.quantum_info import Statevector
For example, the following creates the state \(|7\rangle\) for a 4-qubit register:
= Statevector.from_int(7, 2**4)
state 'latex') state.draw(
\[ |0111\rangle\]
We can visualize the state \(|7\rangle\) on a register of Bloch spheres as follows:
from qiskit.visualization import plot_bloch_multivector
=True) plot_bloch_multivector(state, reverse_bits
We can run this state through a quantum circuit as in the following example:
= QuantumCircuit(4)
qc 1)
qc.h(2)
qc.h(0, 2)
qc.cx(1,3)
qc.swap(='mpl', reverse_bits=True) qc.draw(output
= state.evolve(qc)
state 'latex') state.draw(
\[- \frac{1}{2} |0001\rangle+\frac{1}{2} |0101\rangle+\frac{1}{2} |1001\rangle- \frac{1}{2} |1101\rangle\]
Finally, let’s look at this state using Bloch spheres:
=True) plot_bloch_multivector(state, reverse_bits