-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperdense_coding.py
More file actions
106 lines (79 loc) · 3.01 KB
/
superdense_coding.py
File metadata and controls
106 lines (79 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
from typing import Dict
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import SamplerV2
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("Agg") # Non-interactive backend for headless environments
def create_superdense_circuit(message: str) -> QuantumCircuit:
"""Creates a quantum superdense coding circuit.
Args:
message (str): The 2-bit classical message to send ('00', '01', '10', or '11').
Returns:
QuantumCircuit: The superdense coding circuit.
"""
if message not in ["00", "01", "10", "11"]:
raise ValueError("Message must be one of '00', '01', '10', '11'")
qc = QuantumCircuit(2)
# 1. Create Bell pair (shared between Alice and Bob)
# This is the "pre-shared resource"
qc.h(0)
qc.cx(0, 1)
qc.barrier()
# 2. Alice's encoding
# Alice applies gates to her qubit (0) based on the message
# In Qiskit's bit ordering, for message "m1m0":
# - If m0 == '1', Bob should measure q0=1. This requires Alice to apply Z.
# - If m1 == '1', Bob should measure q1=1. This requires Alice to apply X.
if message[1] == "1": # m0
qc.z(0)
if message[0] == "1": # m1
qc.x(0)
qc.barrier()
# 3. Alice sends her qubit to Bob
# (Simulated by Bob now having access to both qubits 0 and 1)
# 4. Bob's decoding
qc.cx(0, 1)
qc.h(0)
qc.barrier()
# 5. Bob measures
qc.measure_all()
return qc
def run_simulation(qc: QuantumCircuit, message: str) -> Dict[str, int]:
"""Runs the superdense coding circuit on a local AerSimulator.
Args:
qc: The QuantumCircuit to simulate.
message: The original message for display purposes.
Returns:
Dict[str, int]: The measurement counts.
"""
# Ensure output directory exists
os.makedirs("outputs", exist_ok=True)
# Visualize the circuit (saved as an image)
print(f"Drawing the circuit for message '{message}'...")
qc.draw(output="mpl", filename=f"outputs/superdense_circuit_{message}.png")
# Run the simulation using SamplerV2
sim = AerSimulator()
sampler = SamplerV2()
tqc = transpile(qc, sim)
job = sampler.run([(tqc, None, 1024)])
result = job.result()
# Get the counts from the 'meas' register (added by measure_all)
counts = result[0].data.meas.get_counts()
print(f"Results for message '{message}': {counts}")
# Visualize the histogram (saved as an image)
plot_histogram(counts)
plt.savefig(f"outputs/superdense_histogram_{message}.png")
return counts
def main() -> None:
"""Main execution flow for all 4 possible messages."""
messages = ["00", "01", "10", "11"]
for msg in messages:
print(f"\n--- Superdense Coding: Sending '{msg}' ---")
qc = create_superdense_circuit(msg)
run_simulation(qc, msg)
print("\nSimulation complete. Images generated in 'outputs/'.")
if __name__ == "__main__":
main()