Last active
November 23, 2019 13:42
-
-
Save yutakahashi114/ee6c475adf7c436b660402275b22b0e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cirq | |
import random | |
class Alice: | |
def __init__(self, bit_count): | |
self._initial_bits = [random.choice([0, 1]) for _ in range(bit_count)] | |
self.encode_gates = [random.choice([0, 1]) for _ in range(bit_count)] | |
self._secret_key = '' | |
def encode_function(self, index): | |
def encode_function(qubit): | |
initial_bit = self._initial_bits[index] | |
if initial_bit == 1: | |
yield cirq.X(qubit) | |
gate = self.encode_gates[index] | |
# 0:X基底、1:H基底 | |
if gate == 0: | |
yield cirq.X(qubit) | |
else: | |
yield cirq.H(qubit) | |
return encode_function | |
def measure_qubits(self, circuit, qubits): | |
# qubitを測定する | |
circuit.append(cirq.measure(*[qubits[0], qubits[1]], key='alice')) | |
def set_secret_key(self, decode_gates): | |
self._secret_key = '' | |
for index, gate in enumerate(self.encode_gates): | |
# エンコードしたゲートとデコードしたゲートが同じであれば、 | |
# 対応するinitial_bitを秘密鍵のbitに追加 | |
if gate == decode_gates[index]: | |
self._secret_key += str(self._initial_bits[index]) | |
print('alice_secret_key : ' + self._secret_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment