Skip to main content
Logo image

Section 2.4 Coding Quantum Circuits

One extremely valuable tool for creating and visualizing quantum circuits is the Qiskit package for python that was created by IBM. A full overview of Qiskit from its creators is available at https://www.ibm.com/quantum/qiskit, but we will provide a basic introduction here as well.
The package can be added to a python codespace with the import qiskit command or by using from qiskit import [],[],.... A quantum circuit is composed of both a quantum register and a classical register. These can be defined with the QuantumRegister(n) and ClassicalRegister(n) methods where n is the number of bits in the register. Putting these together, we can create a quantum circuit with QuantumCircuit(QuantumRegister(n),ClassicalRegister(n)). Once we have created a quantum circuit, we can visualize it with the .draw() method. The following code shows an example of a quantum circuit with two quantum bits and one classical bit.
Here we define the variables qreg as the quantum register, creg as the classical register, and qcircuit as the quantum circuit. We can see that the two quantum bits we created are indexed 0 and 1 and the classical bit we created has index 0.

Subsection 2.4.1 Gates in Qiskit

Subsubsection 2.4.1.1 One Qubit Gates

In each of the following we will use the same variable names as above and i represents the index of the bit we wish to apply the gate to.
\(\textbf{X-Gate (NOT operator)}\)
\(X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \)
To apply this gate to a qubit, use qcircuit.x(qreg[i])
\(\textbf{Z-Gate (Z operator)}\)
\(Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} \)
To apply this gate to a qubit, use qcircuit.z(qreg[i])
\(\textbf{H-Gate (Hadamard operator)}\)
\(X = \begin{pmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{pmatrix} \)
To apply this gate to a qubit, use qcircuit.h(qreg[i])

Subsubsection 2.4.1.2 Two Qubit Gates

For two qubit gates, we will use i to represent the index of the control qubit and j to represent the index of the target qubit
\(\textbf{CX-Gate (Controlled NOT operator)}\)
\(CX = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} \)
To apply this gate to a pair of qubits, use qcircuit.cx(qreg[i],qreg[j])
This example shows the code to create a quantum circuit with three quantum bits and one classical bit. All three qubits have a Hadamard gate applied to them, the 0th qubit has an X gate applied to it, and the 1st qubit is used as a control for a Controlled Not gate targeting the 2nd qubit.
With the version of Qiskit we are using here, we must use slices instead of integers to specify the indexes of the qbits we want to measure.
In the space below, write code to create a quantum circuit with two qubits and one classical bit. Apply a hadamard gate to the 0th qubit and an X gate to the 1st qubit
Solution.
In the space below, write code to create a quantum circuit with three qubits and one classical bit. Apply an X-gate to the 0th qubit, a hadamard gate to the 1st qubit, and a Controlled X gate using the 2nd qubit as a control and the 0th qubit as a target.
Solution.
This is only a very brief introduction to Qiskit, and there is much more you can do, including running code on a quantum computer simulator to measure a quantum circuit, and connecting to one of IBM’s real quantum computers to run code.