From 8fbcb933a62a40af0f6d4d170b00bd55e9ea4df3 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 14 Oct 2025 17:54:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=B2=D0=B0=D0=BD=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=82=D0=B5=D0=BB=D0=B5=D0=BF=D0=BE=D1=80=D1=82?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task7/main.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 task7/main.py diff --git a/task7/main.py b/task7/main.py new file mode 100644 index 0000000..40a1ef7 --- /dev/null +++ b/task7/main.py @@ -0,0 +1,81 @@ +from __future__ import annotations + +import random + +import cirq +import numpy as np + + +def make_quantum_teleportation_circuit(ranX, ranY): + circuit = cirq.Circuit() + msg, alice, bob = cirq.LineQubit.range(3) + + # Создаём запутанную пару кубитов между Алисой и Бобом + circuit.append([cirq.H(alice), cirq.CNOT(alice, bob)]) + + # Создаём случайное состояние для сообщения + circuit.append([cirq.X(msg) ** ranX, cirq.Y(msg) ** ranY]) + + # Запутываем сообщение и кубит Алисы + circuit.append([cirq.CNOT(msg, alice), cirq.H(msg)]) + + # Измеряем сообщение и кубит Алисы + circuit.append(cirq.measure(msg, key="msg")) + circuit.append(cirq.measure(alice, key="alice")) + + # Используем два классических бита + # для восстановления исходного квантового сообщения на кубите Боба + circuit.append(cirq.X(bob).with_classical_controls("alice")) + circuit.append(cirq.Z(bob).with_classical_controls("msg")) + + return circuit + + +def main(seed=None): + random.seed(seed) + + ranX = random.random() + ranY = random.random() + circuit = make_quantum_teleportation_circuit(ranX, ranY) + + print("Circuit:") + print(circuit) + + sim = cirq.Simulator(seed=seed) + + # Запускаем простую симуляцию, которая применяет случайные X и Y-гейты, + # создающие наше сообщение + q0 = cirq.LineQubit(0) + message = sim.simulate(cirq.Circuit([cirq.X(q0) ** ranX, cirq.Y(q0) ** ranY])) + + print("\nBloch Sphere of Message After Random X and Y Gates:") + expected = cirq.bloch_vector_from_state_vector(message.final_state_vector, 0) + print( + "x: ", + np.around(expected[0], 4), + "y: ", + np.around(expected[1], 4), + "z: ", + np.around(expected[2], 4), + ) + + final_results = sim.simulate(circuit) + + print("\nBloch Sphere of Qubit 2 at Final State:") + teleported = cirq.bloch_vector_from_state_vector( + final_results.final_state_vector, 2 + ) + print( + "x: ", + np.around(teleported[0], 4), + "y: ", + np.around(teleported[1], 4), + "z: ", + np.around(teleported[2], 4), + ) + + return expected, teleported + + +if __name__ == "__main__": + main()