Skip to content

Instantly share code, notes, and snippets.

@GDLMadushanka
Created December 2, 2020 02:12
Show Gist options
  • Save GDLMadushanka/9ebae6b3fff104514f4bc82b3803dba9 to your computer and use it in GitHub Desktop.
Save GDLMadushanka/9ebae6b3fff104514f4bc82b3803dba9 to your computer and use it in GitHub Desktop.
Superdense Coding qiskit
# import all necessary objects and methods for quantum circuits
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer
all_pairs = ['00','01','10','11']
for pair in all_pairs:
# Creating the circuit with two classical bits and 2 qbits
qc = QuantumCircuit(2,2)
# Creating the entangled state
qc.h(1)
qc.cx(1,0)
qc.barrier()
# Alice applying Z,X gates depending on the
# message she needs to send.
# Note : - Alice's qubit - q1
if pair[0] == '1':
qc.z(1)
if pair[1] == '1':
qc.x(1)
qc.barrier()
# Bob applying CNOT and H gate
# Note :- Bob's operation are always the same
qc.cx(1,0)
qc.h(1)
qc.barrier()
# Bob measure both qubits to retrieve the message
qc.measure(0,0)
qc.measure(1,1)
display(qc.draw(output='mpl',reverse_bits=True))
job = execute(qc,Aer.get_backend('qasm_simulator'),shots=1024)
counts = job.result().get_counts(qc)
print("Alice sent :",pair,"Bob received :",counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment