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 testQiskit.py
to our public Quantum GitHub repository here.
Introduction
Open our Quantum project in PyCharm, right-click and create a new Python file called testSuperdenseCoding.py
. 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 testSuperdenseCoding.py
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
mpl.use('TkAgg')
def show_figure(fig):
new_fig = plt.figure()
new_mngr = new_fig.canvas.manager
new_mngr.canvas.figure = fig
fig.set_canvas(new_mngr.canvas)
fig.show(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:
circuit.h(q[0])
Add a CNOT Gate to the circuit with control on the first qubit targetting the second:
circuit.cx(q[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:
circuit.cx(q[0], q[1])
Apply another Hadamard gate to take it out of superposition:
circuit.h(q[0])
Add a measurement at the end:
circuit.measure(q, c)
Plot the circuit:
diagram = circuit.draw(output='mpl')
show_figure(diagram)
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 = datetime.now()
job = execute(circuit, backend, shots=100000)
end_time = datetime.now()
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:

Conclusion
In this article we
References
My Code Repository can be found on my public GitHub:
https://github.com/BenWhiteside/Quantum
This blog’s program can be found under
https://github.com/BenWhiteside/Quantum/testSuperdenseCoding.py
[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)
[3] https://qiskit.org/textbook/ch-states/single-qubit-gates.html#ynzgatez
[4] https://qiskit.org/textbook/ch-algorithms/superdense-coding.html
[6] http://einsteinrelativelyeasy.com/index.php/quantum-mechanics/154-hadamard-gate-on-multiple-qubits
- Nielsen & Chuang, “Quantum Computing and Quantum Information“, Cambridge University Press (2016)
- Hidary, “Quantum Computing: An Applied Approach“, Springer (2019)
- Atkin, “Demystifying Superdense Coding“, http://www.medium.com (2018)
- Qiskit: www.qiskit.org.
- Quantum GitHub.