Skip to content

Instantly share code, notes, and snippets.

@yutakahashi114
Created November 23, 2019 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutakahashi114/6c625166d976128093dd392c76e724f5 to your computer and use it in GitHub Desktop.
Save yutakahashi114/6c625166d976128093dd392c76e724f5 to your computer and use it in GitHub Desktop.
import cirq
import random
class Bob:
def __init__(self):
self._result_bits = []
self.decode_gates = []
self._secret_key = ''
def measure_qubits(self, circuit, qubit):
# 0:X基底、1:H基底
bit = random.choice([0, 1])
self.decode_gates.append(bit)
if bit == 0:
circuit.append(cirq.X(qubit))
else:
circuit.append(cirq.H(qubit))
# qubitを測定する
circuit.append(cirq.measure(*[qubit], key='bob'))
result = cirq.Simulator().run(circuit)
self._result_bits.append(result.measurements['bob'][0][0])
print(circuit)
def set_secret_key(self, encode_gates):
self._secret_key = ''
for index, gate in enumerate(self.decode_gates):
# エンコードしたゲートとデコードしたゲートが同じであれば、
# 対応するresult_bitを秘密鍵のbitに追加
if gate == encode_gates[index]:
self._secret_key += str(self._result_bits[index])
print('bob_secret_key : ' + self._secret_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment