Coding Multi-qubit Circuits in Qiskit

Quantum Circuits, Bell States, and Entanglement

Madeline Farina
QubitCo
5 min readOct 13, 2020

--

Multi-qubit quantum circuit with Hadamard gates, CNOT gates, X gates, etc.

Note: this tutorial is an extension of my tutorial Coding Single qubit circuits in Qiskit, where I include all the necessary set-up code like installation, package importation, and the creation of a quantum circuit. I recommend checking it out first before continuing with this post.

Now that you know how to code quantum circuits with single qubit in Qiskit, it’s time to transition to the complex world of multi-qubit circuits. Again, all the code is quite simple and will be run in a Jupyter Notebook, with code bits and screenshots of output provided.

Coding a 2-qubit circuit

Recall that for N qubits, there are 2^N possible states (or amplitudes, if you were to think of a qubit as a wave function). So for two qubits, there are 2² = 4 possible states:

|00⟩=|0⟩
|01⟩=|1⟩
|10⟩=|2⟩
|11⟩=|3⟩

These basis states can be represented on the Q-sphere (shown below), which is an alternate way of visualizing quantum states since the Bloch Sphere cannot represent general multi-qubit states. The Q-Sphere displays the amplitudes (which are complex numbers) of each computational basis state.

sv = Statevector.from_label('00')
plot_state_qsphere(sv.data)
sv = Statevector.from_label('11')
plot_state_qsphere(sv.data)

And so on. Note that the size of the red orb indicates the magnitude of the amplitude. Given all this information, describing the state of two qubits requires four complex amplitudes, with four degrees of freedom. In general, for two qubits, we can describe their collective state using the tensor product, with the resulting amplitudes stored in a 4D-vector like so:

So for example, for |0⟩ and |1⟩, their collective states are |00⟩, |01⟩ |10⟩, and |11⟩.

Coding a 3+ qubit circuit

The following code creates a 3-qubit circuit, but you can change the 3 with any (sensible) real number and a circuit of that number of qubits with a corresponding amount of Hadamard gates can be created.

qc = QuantumCircuit(3)# Apply H-gate to each qubit:
for qubit in range(3):
qc.h(qubit)
# See the circuit:
qc.draw('mpl')

Output:

Now let’s see the statevector for this circuit.

backend = qiskit.Aer.get_backend('statevector_simulator')
final_state = execute(qc,backend).result().get_statevector()
# In Jupyter Notebooks we can display this nicely using LaTeX.
# If you're not using Jupyter Notebooks you may need to remove the
# array_to_latex function and use print(final_state) instead.
from qiskit_textbook.tools import array_to_latex
array_to_latex(final_state, pretext="\\text{Statevector} = ")

Output:

plot_state_qsphere(final_state)

Take a look at all those states! Notice how the state pairs are antipodal. Now that the basics of multi-qubit circuits have been covered, let’s take a look at an important quantum mechanical concept called entanglement.

Entanglement

Entanglement is a property which, when related to qubits, implies that the states of two or more qubits are dependent on each other. If qubit A is in a state |𝜓𝐴 and qubit B is in a state |𝜙𝐵, the total (bipartite) state is the tensor product of the two individual qubit states (|𝜓𝐴|𝜙𝐵) and is uncorrelated measuring one of the qubits tells us nothing about the state of the other. If the state is uncorrelated, it can be factorized into a tensor product of the individual qubits. However, there exist certain bipartite states:

a₀₀|00⟩+a₀₁|01⟩+a₁₀|10⟩+a₁₁|11⟩

which cannot be written as |𝜓𝐴|𝜙𝐵. These states are said to be correlated and in the extreme case, entangled (i.e., very strongly correlated).

Entanglement is important for quantum computers because quantum algorithms use this property to be more efficient than classical computers. Additionally, any quantum computation which never generates entangled quantum states can therefore be efficiently simulated on a classical computer.

Bell States

The Bell States are a special type of quantum states that are maximally entangled, meaning for the Bell state 1/√2(|00⟩+|11⟩), this state has 50% probability of being measured in the state |00⟩, and 50% chance of being measured in the state |11⟩. Most interestingly, it has a 0% chance of being measured in the states |01⟩ or |10⟩. You can see the other Bell states in the table below:

Table of the Bell States

The Bell states can be created with the following circuit:

# create a quantum circuit with 2 qubits
qc = QuantumCircuit(2)
# Add a Hadamard gate on qubit 0
qc.h(0)
# Add CNOT with the 0 bit as the control bit and the 1 bit as the target bit
qc.cx(0,1)
# Draw the circuit
qc.draw('mpl')

Output:

If you want to check the probabilities are indeed 50/50, you can run the following code:

results = execute(qc,backend).result().get_counts()
plot_histogram(results)

Which should output the following diagram:

Realize that this combined state cannot be written as two separate qubit states, which actually has interesting implications. If we were to measure the state of one qubit, it would tell us the state of the other because of superposition, and then collapse its superposition. Even if the qubits were separated by a large distance, measuring one qubit would till collapse the superposition and have an instant effect on the other qubit. This phenomenon is called the ‘spooky action at a distance’ and was a problem for many physicists in the early 20th century.

Also realize that the measurement result is random, and the measurement statistics of one qubit are not at all affected by any operation on the other qubit. Because of this, it is impossible to use shared quantum states to communicate. This is known as the no-communication theorem.

Sources

For any additional questions, the documentation for Qiskit can be found here here, and the official Qiskit textbook can be found here, which is what I regularly referenced.

--

--

Madeline Farina
QubitCo

Quantum Physics, InfoSec, and general scientific nonsense