def test_ugates_cirq(): c1 = Circuit(3) c1.add(gates.RX(0, 0.1)) c1.add(gates.RZ(1, 0.4)) c1.add(gates.U2(2, 0.5, 0.6)) final_state_c1 = c1() c2 = circuit_from_qasm(c1.to_qasm()) c2depth = len(cirq.Circuit(c2.all_operations())) assert c1.depth == c2depth final_state_c2 = cirq.Simulator().simulate(c2).final_state np.testing.assert_allclose(final_state_c1, final_state_c2, atol=_atol) c3 = Circuit.from_qasm(c2.to_qasm()) assert c3.depth == c2depth final_state_c3 = c3() np.testing.assert_allclose(final_state_c3, final_state_c2, atol=_atol) c1 = Circuit(3) c1.add(gates.RX(0, 0.1)) c1.add(gates.RZ(1, 0.4)) c1.add(gates.U2(2, 0.5, 0.6)) c1.add(gates.CU3(2, 1, 0.2, 0.3, 0.4)) # catches unknown gate "cu3" with pytest.raises(exception.QasmException): c2 = circuit_from_qasm(c1.to_qasm())
def test_ugates_cirq(backend): import qibo original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(3) c1.add(gates.RX(0, 0.1)) c1.add(gates.RZ(1, 0.4)) c1.add(gates.U2(2, 0.5, 0.6)) final_state_c1 = c1() c2 = circuit_from_qasm(c1.to_qasm()) c2depth = len(cirq.Circuit(c2.all_operations())) assert c1.depth == c2depth final_state_c2 = cirq.Simulator().simulate(c2).final_state_vector # pylint: disable=no-member np.testing.assert_allclose(final_state_c1, final_state_c2, atol=_atol) c3 = Circuit.from_qasm(c2.to_qasm()) assert c3.depth == c2depth final_state_c3 = c3() np.testing.assert_allclose(final_state_c3, final_state_c2, atol=_atol) c1 = Circuit(3) c1.add(gates.RX(0, 0.1)) c1.add(gates.RZ(1, 0.4)) c1.add(gates.U2(2, 0.5, 0.6)) c1.add(gates.CU3(2, 1, 0.2, 0.3, 0.4)) # catches unknown gate "cu3" with pytest.raises(exception.QasmException): c2 = circuit_from_qasm(c1.to_qasm()) qibo.set_backend(original_backend)
def test_controlled_u3(backend): theta, phi, lam = 0.1, 0.1234, 0.4321 initial_state = random_state(2) c = Circuit(2) c.add(gates.U3(1, theta, phi, lam).controlled_by(0)) final_state = c(np.copy(initial_state)) assert c.queue[0].__class__.__name__ == "CU3" c = Circuit(2) c.add(gates.CU3(0, 1, theta, phi, lam)) target_state = c(np.copy(initial_state)) K.assert_allclose(final_state, target_state) # for coverage gate = gates.U3(0, theta, phi, lam) assert gate.parameters == (theta, phi, lam)
def test_ugates(): c = Circuit(3) c.add(gates.RX(0, 0.1)) c.add(gates.RZ(1, 0.4)) c.add(gates.U2(2, 0.5, 0.6)) c.add(gates.CU1(0, 1, 0.7)) c.add(gates.CU3(2, 1, 0.2, 0.3, 0.4)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; rx(0.1) q[0]; rz(0.4) q[1]; u2(0.5, 0.6) q[2]; cu1(0.7) q[0],q[1]; cu3(0.2, 0.3, 0.4) q[2],q[1];""" assert_strings_equal(c.to_qasm(), target) c = Circuit(2) c.add(gates.CU2(0, 1, 0.1, 0.2)) with pytest.raises(ValueError): target = c.to_qasm()
def test_controlled_u3(backend): """Check controlled U3 fall backs to CU3.""" original_backend = qibo.get_backend() qibo.set_backend(backend) theta = 0.1 phi = 0.1234 lam = 0.4321 initial_state = utils.random_numpy_state(2) c = Circuit(2) c.add(gates.U3(1, theta, phi, lam).controlled_by(0)) final_state = c(np.copy(initial_state)) assert c.queue[0].__class__.__name__ == "CU3" c = Circuit(2) c.add(gates.CU3(0, 1, theta, phi, lam)) target_state = c(np.copy(initial_state)) np.testing.assert_allclose(final_state, target_state) # for coverage gate = gates.U3(0, theta, phi, lam) assert gate.parameter == (theta, phi, lam)