Created
July 5, 2021 23:25
-
-
Save chizuchizu/69a9428f35a5bd6959efdd752e6bf183 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 | |
# Number of qubits n. | |
nqubits = 3 | |
# Grover's operatorを何回作用させるか | |
num_loop = 2 | |
# 正解のbit | |
target_bits = [ | |
1, 0, 0 | |
] | |
repetitions = 1000 | |
qubits = cirq.LineQubit.range(nqubits) | |
def make_oracle(input_qubits, output_qubit, x_bits): | |
return [ | |
[cirq.X(q) for (q, bit) in zip(input_qubits, x_bits) if not bit], | |
# cirq.X.controlled(len(input_qubits))(*input_qubits, output_qubit), | |
cirq.Z.controlled(len(input_qubits) - 1)(*input_qubits), | |
[cirq.X(q) for (q, bit) in zip(input_qubits, x_bits) if not bit] | |
] | |
def make_grover_operator(input_qubits, output_qubit, oracle): | |
return [ | |
oracle, | |
cirq.H.on_each(*input_qubits), | |
cirq.X.on_each(*input_qubits), | |
cirq.Z.controlled(len(input_qubits) - 1)(*input_qubits), | |
cirq.X.on_each(*input_qubits), | |
cirq.H.on_each(*input_qubits) | |
] | |
def make_grover_circuit(input_qubits, output_qubit, oracle, grover_operator): | |
c = cirq.Circuit() | |
# 初期設定 | |
c.append( | |
[ | |
cirq.H.on_each(*input_qubits), | |
] | |
) | |
# Grover's operatorを適当な回数、作用させる | |
[c.append(grover_operator) for _ in range(num_loop)] | |
# 結果を測定 | |
c.append(cirq.measure(*input_qubits, key='result')) | |
return c | |
def bitstring(bits): | |
return ''.join(str(int(b)) for b in bits) | |
circuit = cirq.Circuit() | |
oracle = make_oracle(qubits, None, target_bits) | |
grover_operator = make_grover_operator(qubits, None, oracle) | |
circuit = make_grover_circuit(qubits, None, oracle, grover_operator) | |
# 量子回路 | |
print(circuit) | |
"""-------------""" | |
print("-" * 30) | |
print("parameters") | |
print(f"Target bits: {bitstring(target_bits)}") | |
print(f"num_loop: {num_loop}") | |
print(f"repetitions: {repetitions}") | |
print("-" * 30) | |
print() | |
"""-------------""" | |
simulator = cirq.Simulator() | |
result = simulator.run(circuit, repetitions=repetitions) | |
frequencies = result.histogram(key='result', fold_func=bitstring) | |
print(f'Sampled results:\n{frequencies}') | |
# Check if we actually found the secret value. | |
most_common_bitstring = frequencies.most_common(1)[0][0] | |
most_common_bitstring_prob = frequencies.most_common(1)[0][1] / repetitions * 100 | |
print(f'Most common bitstring: {most_common_bitstring} ({most_common_bitstring_prob} %)') | |
equal_flag = bitstring(target_bits) == most_common_bitstring | |
print("Found a match:", equal_flag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment