def main(): ranX = float(input('X = ')) ranY = float(input('Y = ')) circuit = makeQuantumTeleportationCircuit(ranX, ranY) print("Circuit:") print(circuit) sim = cirq.Simulator() q0, q1 = cirq.LineQubit.range(2) message = sim.simulate(cirq.Circuit.from_ops( [cirq.X(q0)**ranX, cirq.Y(q1)**ranY])) print("\nBloch Sphere of Message After X and Y Gates:") b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector( message.final_state, 0) print("x: ", np.around(b0X, 4), "y: ", np.around(b0Y, 4), "z: ", np.around(b0Z, 4)) final_results = sim.simulate(circuit) print("\nBloch Sphere of Qubit 2 at Final State:") b2X, b2Y, b2Z = cirq.bloch_vector_from_state_vector( final_results.final_state, 2) print("x: ", np.around(b2X, 4), "y: ", np.around(b2Y, 4), "z: ", np.around(b2Z, 4))
def main(): # 원격이동할 무작위 상태를 부호화합니다. ranX = random.random() ranY = random.random() msg, circuit = make_quantum_teleportation_circuit(ranX, ranY) # 회로를 시뮬레이션합니다. sim = cirq.Simulator() message = sim.simulate(cirq.Circuit([cirq.X(msg)**ranX, cirq.Y(msg)**ranY])) # 엘리스의 큐비트를 블로흐 구 위에 출력합니다. print("Bloch Sphere of Alice's qubit:") b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector(message.final_state, 0) print("x: ", round(b0X, 4), "y: ", round(b0Y, 4), "z: ", round(b0Z, 4)) # 원격이동 회로를 출력합니다. print("\nCircuit:") print(circuit) # 시뮬레이션의 마지막 상태를 저장합니다. final_results = sim.simulate(circuit) # 밥의 큐비트를 블로흐 구 위에 출력합니다. print("\nBloch Sphere of Bob's qubit:") b2X, b2Y, b2Z = cirq.bloch_vector_from_state_vector( final_results.final_state, 2) print("x: ", round(b2X, 4), "y: ", round(b2Y, 4), "z: ", round(b2Z, 4))
def main(): ranX = random.random() ranY = random.random() circuit = make_quantum_teleportation_circuit(ranX, ranY) print("Circuit:") print(circuit) sim = cirq.Simulator() # Create qubits. q0 = cirq.LineQubit(0) # Produces the message using random X and Y gates message = sim.simulate(cirq.Circuit([cirq.X(q0)**ranX, cirq.Y(q0)**ranY])) print("\nBloch Sphere of Message After Random X and Y Gates:") # Prints the Bloch Sphere of the Message after the X and Y gates expected = cirq.bloch_vector_from_state_vector(message.final_state, 0) print("x: ", np.around(expected[0], 4), "y: ", np.around(expected[1], 4), "z: ", np.around(expected[2], 4)) # Records the final state of the simulation final_results = sim.simulate(circuit) print("\nBloch Sphere of Qubit 2 at Final State:") # Prints the Bloch Sphere of Bob's entangled qubit at the final state teleported = cirq.bloch_vector_from_state_vector(final_results.final_state, 2) print("x: ", np.around(teleported[0], 4), "y: ", np.around(teleported[1], 4), "z: ", np.around(teleported[2], 4)) return expected, teleported
def test_bloch_vector_invalid(): with pytest.raises(ValueError): _ = cirq.bloch_vector_from_state_vector(np.array([0.5, 0.5, 0.5]), 0) with pytest.raises(IndexError): _ = cirq.bloch_vector_from_state_vector(np.array([0.5, 0.5, 0.5, 0.5]), -1) with pytest.raises(IndexError): _ = cirq.bloch_vector_from_state_vector(np.array([0.5, 0.5, 0.5, 0.5]), 2)
def main(): # Encode a random state to teleport ranX = random.random() ranY = random.random() msg, circuit = make_quantum_teleportation_circuit(ranX, ranY) # Simulate the circuit sim = cirq.Simulator() message = sim.simulate(cirq.Circuit([cirq.X(msg)**ranX, cirq.Y(msg)**ranY])) # Print the Bloch Sphere of Alice's qubit print("Bloch Sphere of Alice's qubit:") b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector(message.final_state, 0) print("x: ", round(b0X, 4), "y: ", round(b0Y, 4), "z: ", round(b0Z, 4)) # Display the teleportation circuit print("\nCircuit:") print(circuit) # Record the final state of the simulation final_results = sim.simulate(circuit) # Print the Bloch sphere of Bob's qubit print("\nBloch Sphere of Bob's qubit:") b2X, b2Y, b2Z = cirq.bloch_vector_from_state_vector( final_results.final_state, 2) print("x: ", round(b2X, 4), "y: ", round(b2Y, 4), "z: ", round(b2Z, 4))
def test_bloch_vector_multi_pure(): plus_plus_state = np.array([0.5, 0.5, 0.5, 0.5]) bloch_0 = cirq.bloch_vector_from_state_vector(plus_plus_state, 0) bloch_1 = cirq.bloch_vector_from_state_vector(plus_plus_state, 1) desired_simple = np.array([1, 0, 0]) np.testing.assert_array_almost_equal(bloch_1, desired_simple) np.testing.assert_array_almost_equal(bloch_0, desired_simple)
def main(seed=None): """Run a simple simulation of quantum teleportation. Args: seed: The seed to use for the simulation. """ random.seed(seed) ranX = random.random() ranY = random.random() # Run a simple simulation that applies the random X and Y gates that # create our message. q0 = cirq.LineQubit(0) simple_circuit = cirq.Circuit([cirq.X(q0)**ranX, cirq.Y(q0)**ranY]) simple_sim = cirq.KnowledgeCompilationSimulator(simple_circuit) message = simple_sim.simulate(simple_circuit) # Records the final state of the simulation. circuit = make_quantum_teleportation_circuit(ranX, ranY) print("Circuit:") print(circuit) sim = cirq.KnowledgeCompilationSimulator(circuit) final_results = sim.simulate(circuit) print("\nBloch Sphere of Message After Random X and Y Gates:") # Prints the Bloch Sphere of the Message after the 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), ) print("\nBloch Sphere of Qubit 2 at Final State:") # Prints the Bloch Sphere of Bob's entangled qubit at the 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
def main(): ranX = random.random() ranY = random.random() ranX2 = random.random() ranY2 = random.random() circuit = make_circuit(ranX, ranY, ranX2, ranY2) msg = [cirq.LineQubit(7), cirq.LineQubit(8)] sim = cirq.Simulator() a = cirq.Circuit() a.append([ cirq.X(msg[0])**ranX, cirq.Y(msg[0])**ranY, cirq.X(msg[1])**ranX2, cirq.Y(msg[1])**ranY2 ]) message = sim.simulate(a) received = sim.simulate(circuit) print("앨리스의 큐비트") b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector(message.final_state, 0) print( "\nx: ", round(b0X, 3), "y: ", round(b0Y, 3), "z: ", round(b0Z, 3), ) b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector(message.final_state, 1) print( "\nx: ", round(b0X, 3), "y: ", round(b0Y, 3), "z: ", round(b0Z, 3), ) print(message) print("\n\n전송된 큐비트") b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector( received.final_state, 4) print("\nx: ", round(b0X, 3), "y: ", round(b0Y, 3), "z: ", round(b0Z, 3)) b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector( received.final_state, 5) print("\nx: ", round(b0X, 3), "y: ", round(b0Y, 3), "z: ", round(b0Z, 3)) print(received)
def main(): ranX=random.random() ranY=random.random() msg,circuit=make_quantum_teleportation_circuit(ranX,ranY) sim=cirq.Simulator() message=sim.simulate(cirq.Circuit([cirq.X(msg)**ranX,cirq.Y(msg)**ranY])) print("Bloch sphere of Alice's qubit: ") b0X,b0Y,b0Z=cirq.bloch_vector_from_state_vector(message.final_state,0) print("x: ",round(b0X,4),"y: ",round(b0Y,4),"z: ",round(b0Z,4)) print("\nCircuit") print(circuit) final_results=sim.simulate(circuit) print("\nBloch sphere of Bob's qubit: ") b2X,b2Y,b2Z=cirq.bloch_vector_from_state_vector(final_results.final_state,2) print("x: ",round(b2X,4),"y: ",round(b2Y,4),"z: ",round(b2Z,4))
def __init__(self, sphere_radius: int = 5, state_vector: cirq.STATE_VECTOR_LIKE = None): """Initializes a BlochSphere. Also initializes it's parent class Widget with the bundle file provided. Args: sphere_radius: the radius of the bloch sphere in the three.js diagram. The default value is 5. state_vector: a state vector to pass in to be represented. Raises: ValueError: If the `sphere_radius` is not positive or the `state_vector` is not supplied. """ super().__init__() if sphere_radius <= 0: raise ValueError('You must input a positive radius for the sphere') self.sphere_radius = sphere_radius if state_vector is None: raise ValueError( 'No state vector given in BlochSphere initialization') self.bloch_vector = cirq.bloch_vector_from_state_vector( cirq.to_valid_state_vector(state_vector, num_qubits=1), 0)
def test_bloch_vector_equal_sqrt3(): sqrt3 = 1 / np.sqrt(3) test_state = np.array([0.888074, 0.325058 + 0.325058j]) bloch = cirq.bloch_vector_from_state_vector(test_state, 0) desired_simple = np.array([sqrt3, sqrt3, sqrt3]) np.testing.assert_array_almost_equal(bloch, desired_simple)
def test_bloch_vector_iminus_state(global_phase): sqrt = np.sqrt(0.5) iminus_state = global_phase * np.array([sqrt, -1j * sqrt]) bloch = cirq.bloch_vector_from_state_vector(iminus_state, 0) desired_simple = np.array([0, -1, 0]) np.testing.assert_array_almost_equal(bloch, desired_simple)
def test_bloch_vector_multi_big(): five_qubit_plus_state = np.array([0.1767767] * 32) desired_simple = np.array([1, 0, 0]) for qubit in range(5): bloch_i = cirq.bloch_vector_from_state_vector(five_qubit_plus_state, qubit) np.testing.assert_array_almost_equal(bloch_i, desired_simple)
def test_bloch_vector_simple_TH_zero(): sqrt = np.sqrt(0.5) TH_state = np.array([sqrt, 0.5+0.5j]) bloch = cirq.bloch_vector_from_state_vector(TH_state, 0) desired_simple = np.array([sqrt,sqrt,0]) np.testing.assert_array_almost_equal(bloch, desired_simple)
def test_bloch_vector_simple_ZH_zero(): sqrt = np.sqrt(0.5) ZH_state = np.array([sqrt, -sqrt]) bloch = cirq.bloch_vector_from_state_vector(ZH_state, 0) desired_simple = np.array([-1,0,0]) np.testing.assert_array_almost_equal(bloch, desired_simple)
def test_bloch_vector_multi_mixed(): sqrt = np.sqrt(0.5) # Bell state 1/sqrt(2)(|00>+|11>) phi_plus = np.array([sqrt, 0.0, 0.0, sqrt]) bloch_0 = cirq.bloch_vector_from_state_vector(phi_plus, 0) bloch_1 = cirq.bloch_vector_from_state_vector(phi_plus, 1) zero = np.zeros(3) np.testing.assert_array_almost_equal(bloch_0, zero) np.testing.assert_array_almost_equal(bloch_1, zero) rcnot_state = np.array([0.90612745, -0.07465783j, -0.37533028j, 0.18023996]) bloch_mixed_0 = cirq.bloch_vector_from_state_vector(rcnot_state, 0) bloch_mixed_1 = cirq.bloch_vector_from_state_vector(rcnot_state, 1) true_mixed_0 = np.array([0.0, -0.6532815, 0.6532815]) true_mixed_1 = np.array([0.0, 0.0, 0.9238795]) np.testing.assert_array_almost_equal(true_mixed_0, bloch_mixed_0) np.testing.assert_array_almost_equal(true_mixed_1, bloch_mixed_1)
def test_bloch_vector_multi_mixed(): sqrt = np.sqrt(0.5) HCNOT_state = np.array([sqrt, 0., 0., sqrt]) bloch_0 = cirq.bloch_vector_from_state_vector(HCNOT_state, 0) bloch_1 = cirq.bloch_vector_from_state_vector(HCNOT_state, 1) zero = np.zeros(3) np.testing.assert_array_almost_equal(bloch_0, zero) np.testing.assert_array_almost_equal(bloch_1, zero) RCNOT_state = np.array([0.90612745, -0.07465783j, -0.37533028j, 0.18023996]) bloch_mixed_0 = cirq.bloch_vector_from_state_vector(RCNOT_state, 0) bloch_mixed_1 = cirq.bloch_vector_from_state_vector(RCNOT_state, 1) true_mixed_0 = np.array([0., -0.6532815, 0.6532815]) true_mixed_1 = np.array([0., 0., 0.9238795]) np.testing.assert_array_almost_equal(true_mixed_0, bloch_mixed_0) np.testing.assert_array_almost_equal(true_mixed_1, bloch_mixed_1)
def test_deprecated(): state_vector = np.array([1, 1], dtype=np.complex64) / np.sqrt(2) with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'): # pylint: disable=unexpected-keyword-arg,no-value-for-parameter _ = cirq.bloch_vector_from_state_vector(state=state_vector, index=0) with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'): # pylint: disable=unexpected-keyword-arg,no-value-for-parameter _ = cirq.density_matrix_from_state_vector(state=state_vector) with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'): # pylint: disable=unexpected-keyword-arg,no-value-for-parameter _ = cirq.dirac_notation(state=state_vector) with cirq.testing.assert_logs( 'validate_normalized_state', 'validate_normalized_state_vector', 'deprecated' ): _ = cirq.validate_normalized_state(state_vector, qid_shape=(2,)) with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'): # pylint: disable=unexpected-keyword-arg,no-value-for-parameter _ = cirq.validate_qid_shape(state=state_vector, qid_shape=(2,))
def test_bloch_vector_one_state(global_phase): one_state = global_phase * np.array([0, 1]) bloch = cirq.bloch_vector_from_state_vector(one_state, 0) desired_simple = np.array([0, 0, -1]) np.testing.assert_array_almost_equal(bloch, desired_simple)
def test_valid_bloch_sphere_vector(): state_vector = np.array([1 / math.sqrt(2), 1 / math.sqrt(2)]) bloch_sphere = cirq_web.BlochSphere(state_vector=state_vector) bloch_vector = cirq.bloch_vector_from_state_vector(state_vector, 0) assert np.array_equal(bloch_vector, bloch_sphere.bloch_vector)