In the last article we built a very simple, 2-qubit quantum circuit with two gates in order to entangle two qubits. We saved this file as to our public Quantum GitHub repository here.


Open our Quantum project in PyCharm, right-click and create a new Python file called PyCharm should prompt you Add File to Git. Click Add.

Type in the following basic imports and a simple 2-qubit quantum register set up:

from qiskit import QuantumCircuit as qc 
from qiskit import QuantumRegister as qr 
from qiskit import ClassicalRegister as cr 
from qiskit import execute, Aer 

# Create two registers: one quantum and one classical: 
q = qr(2) 
c = cr(2) 
circuit = qc(q, c)

Run the code to see if it compiles:


Commit the file to the Quantum GitHub by right-clicking the file in the Project Explorer and selecting Git > Commit. In the Commit Changes pop-up window, tick the file and enter a commit message. Then click Commit. Push the commit by going VCS > Git > Push (Ctrl-Shift-K) and clicking Push. Enter your credentials. Wait for PyCharm to push. Wait for the Push Successful pop up.

Go to the Quantum GitHub repo and hit refresh. The new file should show up.

The same should show up as a new file in GitKraken:


Let’s open our Quantum Computing Glo Board in GitKraken and move our Superdense coding task in to In Progress and Add the File from our local repo to the task:


Let’s also add in our show_figure() function that uses:

import matplotlib as mpl 
import matplotlib.pyplot as plt 
from qiskit.visualization import plot_histogram 


def show_figure(fig): 
    new_fig = plt.figure() 
    new_mngr = new_fig.canvas.manager 
    new_mngr.canvas.figure = fig 

We begin our source code by providing a backend:

backend = Aer.get_backend('qasm_simulator') 
print('Provider: ', backend)

Create two registers: one with 2 qubits and one with 2 classical bits:

q = qr(2, 'q') 
c = cr(2, 'c') 
circuit = qc(q, c)

Add a Hadamard Gate to the first qubit to create a superposition:


Add a CNOT Gate to the circuit with control on the first qubit targetting the second:[0], q[1])

Add one of these gates depending on which message you want to send (let’s assume, for this example, that we want to send the 2-bit state ’01’ using just one bit. So we use the Z gate):

# | Message | Pauli Gate | Implementation                   | 
# | 00      | iden       | circuit.iden(q[0])               | 
# | 01      | Z          | circuit.z(q[0])                  | 
# | 10      | X          | circuit.x(q[0])                  | 
# | 11      | ZX         | circuit.z(q[0]), circuit.x(q[0]) | circuit.z(q[0])

Applying another CNOT gate to entangle the message and the second communication qubit:[0], q[1])

Apply another Hadamard gate to take it out of superposition:


Add a measurement at the end:

circuit.measure(q, c)

Plot the circuit:

diagram = circuit.draw(output='mpl')


 The quantum circuit to encode the 2-bit state ’01’ and send it using just 1 bit.

Execute the circuit on the QASM Simulator and time it:

start_time =
job = execute(circuit, backend, shots=100000) 
end_time = 
print('Duration: {}'.format(end_time - start_time))

Get results:

result = job.result() 
counts = result.get_counts(circuit) 
plot_histogram(counts) show_figure(plot_histogram(result.get_counts(circuit)))

Running the program takes 0.014763 with 100,000 simulations:

We can figure out that the 2-bit state ’01’ was sent by sending just one bit because the result was 100% ’01’ as per the above histogram.


In this article we


My Code Repository can be found on my public GitHub:

This blog’s program can be found under

[1] Chapter 2.3 of Nielsen & Chuang “Quantum Computation and Quantum Infortmation” (2016)

[2] Chapter 7.2 of Hidary “Quantum Computing: An Applied Approach” (2019)



[5] Koch, Wessing & Alsing “Introduction to Coding Quantum Algorithms: A Tutorial Series Using Qiskit” (2019)