Skip to content

Instantly share code, notes, and snippets.

@datafatmunger
Created July 9, 2020 09:08
Show Gist options
  • Save datafatmunger/083ccae6046266d86f3f1ac466200e57 to your computer and use it in GitHub Desktop.
Save datafatmunger/083ccae6046266d86f3f1ac466200e57 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'
q = QuantumRegister(1) # a qubit in which to encode and manipulate the input
c = ClassicalRegister(1) # a bit to store the output
qc = QuantumCircuit(q, c) # this is where the quantum program goes
# Initialize the value of the bit to 1, toggle with comment - JBG
qc.x(q[0])
# Hadamard boterham een, bodem - JBG
qc.h(q[0])
# Encrypt the bit with "secret" - JBG
apply_secret_unitary(secret_unitary, q[0], qc, dagger = 0)
# This is the bit flip ideally done by Bob on another qubit, maybe on another computer - JBG
qc.x(q[0])
# Decrypt the bit with "secret" - JBG
apply_secret_unitary(secret_unitary, q[0], qc, dagger = 1)
# Hadamard boterham twee - JBG
qc.h(q[0])
qc.measure(q[0], c[0])
# We'll run the program on a simulator
backend = Aer.get_backend('qasm_simulator')
# Since the output will be deterministic, we can use just a single shot to get it
job = execute(qc,backend,shots=1024)
output = next(iter(job.result().get_counts()))
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment