Skip to content

Commit a536279

Browse files
Create superdense_coding.py (TheAlgorithms#7349)
* Create superdense_coding.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a3383ce commit a536279

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

Diff for: quantum/superdense_coding.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Build the superdense coding protocol. This quantum
3+
circuit can send two classical bits using one quantum
4+
bit. This circuit is designed using the Qiskit
5+
framework. This experiment run in IBM Q simulator
6+
with 1000 shots.
7+
.
8+
References:
9+
https://qiskit.org/textbook/ch-algorithms/superdense-coding.html
10+
https://en.wikipedia.org/wiki/Superdense_coding
11+
"""
12+
13+
import math
14+
15+
import qiskit
16+
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
17+
18+
19+
def superdense_coding(bit_1: int = 1, bit_2: int = 1) -> qiskit.result.counts.Counts:
20+
"""
21+
The input refer to the classical message
22+
that you wants to send. {'00','01','10','11'}
23+
result for default values: {11: 1000}
24+
┌───┐ ┌───┐
25+
qr_0: ─────┤ X ├──────────┤ X ├─────
26+
┌───┐└─┬─┘┌───┐┌───┐└─┬─┘┌───┐
27+
qr_1: ┤ H ├──■──┤ X ├┤ Z ├──■──┤ H ├
28+
└───┘ └───┘└───┘ └───┘
29+
cr: 2/══════════════════════════════
30+
Args:
31+
bit_1: bit 1 of classical information to send.
32+
bit_2: bit 2 of classical information to send.
33+
Returns:
34+
qiskit.result.counts.Counts: counts of send state.
35+
>>> superdense_coding(0,0)
36+
{'00': 1000}
37+
>>> superdense_coding(0,1)
38+
{'01': 1000}
39+
>>> superdense_coding(-1,0)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: inputs must be positive.
43+
>>> superdense_coding(1,'j')
44+
Traceback (most recent call last):
45+
...
46+
TypeError: inputs must be integers.
47+
>>> superdense_coding(1,0.5)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: inputs must be exact integers.
51+
>>> superdense_coding(2,1)
52+
Traceback (most recent call last):
53+
...
54+
ValueError: inputs must be less or equal to 1.
55+
"""
56+
if (type(bit_1) == str) or (type(bit_2) == str):
57+
raise TypeError("inputs must be integers.")
58+
if (bit_1 < 0) or (bit_2 < 0):
59+
raise ValueError("inputs must be positive.")
60+
if (math.floor(bit_1) != bit_1) or (math.floor(bit_2) != bit_2):
61+
raise ValueError("inputs must be exact integers.")
62+
if (bit_1 > 1) or (bit_2 > 1):
63+
raise ValueError("inputs must be less or equal to 1.")
64+
65+
# build registers
66+
qr = QuantumRegister(2, "qr")
67+
cr = ClassicalRegister(2, "cr")
68+
69+
quantum_circuit = QuantumCircuit(qr, cr)
70+
71+
# entanglement the qubits
72+
quantum_circuit.h(1)
73+
quantum_circuit.cx(1, 0)
74+
75+
# send the information
76+
c_information = str(bit_1) + str(bit_2)
77+
78+
if c_information == "11":
79+
quantum_circuit.x(1)
80+
quantum_circuit.z(1)
81+
elif c_information == "10":
82+
quantum_circuit.z(1)
83+
elif c_information == "01":
84+
quantum_circuit.x(1)
85+
else:
86+
quantum_circuit.i(1)
87+
88+
# unentangled the circuit
89+
quantum_circuit.cx(1, 0)
90+
quantum_circuit.h(1)
91+
92+
# measure the circuit
93+
quantum_circuit.measure(qr, cr)
94+
95+
backend = Aer.get_backend("qasm_simulator")
96+
job = execute(quantum_circuit, backend, shots=1000)
97+
98+
return job.result().get_counts(quantum_circuit)
99+
100+
101+
if __name__ == "__main__":
102+
print(f"Counts for classical state send: {superdense_coding(1,1)}")

0 commit comments

Comments
 (0)