Skip to content

Instantly share code, notes, and snippets.

@datafatmunger
Last active July 9, 2020 14:10
Show Gist options
  • Save datafatmunger/680bf0581e5a3b60523387c6f3b3c9b9 to your computer and use it in GitHub Desktop.
Save datafatmunger/680bf0581e5a3b60523387c6f3b3c9b9 to your computer and use it in GitHub Desktop.
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer, IBMQ, Aer
def apply_secret_unitary(secret_unitary, qubit, quantum_circuit, dagger):
functionmap = {
'x': quantum_circuit.x,
'y': quantum_circuit.y,
'z': quantum_circuit.z,
'h': quantum_circuit.h,
't': quantum_circuit.t
}
if dagger == 1:
functionmap['t'] = quantum_circuit.tdg
if dagger:
[functionmap[unitary](qubit) for unitary in secret_unitary]
else:
[functionmap[unitary](qubit) for unitary in secret_unitary[::-1]]
#secret_unitary = 'hzxhzhx'
secret_unitary = 'ytytyyyzxzx'
crz, crx, result = ClassicalRegister(1), ClassicalRegister(1), ClassicalRegister(1)
q = QuantumRegister(5)
qc = QuantumCircuit(q, crz, crx, result)
# Initialize the value of the bit to 1, toggle with comment - JBG
#qc.x(0)
#
## Encrypt the bit with "secret" - JBG
apply_secret_unitary(secret_unitary, 0, qc, dagger = 0)
#### NEXT!!!!! ... now TELEPORT qubit 0 to qubit 2 - JBG
qc.h(1)
qc.cx(1, 2)
qc.cx(0, 1)
qc.measure(1, 1)
qc.x(2).c_if(crx, 1)
qc.h(0)
qc.measure(0, 0)
qc.z(2).c_if(crz, 1)
## Bob bit flips qubit 2 - JBG
qc.x(2)
### NEXT!!!!! ... now TELEPORT qubit 2 back to qubit 0 - JBG
qc.h(3)
qc.cx(3, 4)
qc.cx(2, 3)
qc.measure(3, 1)
qc.x(4).c_if(crx, 1)
qc.h(2)
qc.measure(2, 0)
qc.z(4).c_if(crz, 1)
## Decrypt the qubit 0 with "secret" - JBG
apply_secret_unitary(secret_unitary, 4, qc, dagger = 1)
qc.measure(4, 2)
counts = execute(qc, Aer.get_backend('qasm_simulator')).result().get_counts()
print(counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment