def test_from_qasm_parametrized_gates(): import numpy as np target = """OPENQASM 2.0; qreg q[2]; rx(0.1234) q[0]; rz(0.4321) q[1]; cu1(0.567) q[0],q[1];""" c = Circuit.from_qasm(target) assert c.depth == 2 assert isinstance(c.queue[0], gates.RX) assert isinstance(c.queue[1], gates.RZ) assert isinstance(c.queue[2], gates.CU1) c2 = Circuit(2) c2.add([gates.RX(0, 0.1234), gates.RZ(1, 0.4321), gates.CU1(0, 1, 0.567)]) np.testing.assert_allclose(c2().numpy(), c().numpy())
def test_simple_cirq(backend): c1 = Circuit(2) c1.add(gates.H(0)) c1.add(gates.H(1)) 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)
def test_from_qasm_measurements(): target = """OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg a[3]; creg b[2]; measure q[0] -> a[0]; x q[3]; measure q[1] -> b[0]; measure q[2] -> a[1]; measure q[4] -> a[2]; measure q[3] -> b[1];""" c = Circuit.from_qasm(target) assert c.depth == 1 assert isinstance(c.queue[0], gates.X) assert isinstance(c.measurement_gate, gates.M) assert c.measurement_tuples == {"a": (0, 2, 4), "b": (1, 3)}
def test_from_qasm_ugates(): import numpy as np target = """OPENQASM 2.0; qreg q[2]; u1(0.1) q[0]; u2(0.2,0.6) q[1]; cu3(0.3,0.4,0.5) q[0],q[1];""" c = Circuit.from_qasm(target) assert c.depth == 2 assert isinstance(c.queue[0], gates.U1) assert isinstance(c.queue[1], gates.U2) assert isinstance(c.queue[2], gates.CU3) c2 = Circuit(2) c2.add([gates.U1(0, 0.1), gates.U2(1, 0.2, 0.6)]) c2.add(gates.U3(1, 0.3, 0.4, 0.5).controlled_by(0)) np.testing.assert_allclose(c2().numpy(), c().numpy())
def test_from_qasm_invalid_measurements(): # Undefined qubit target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[2] -> a[0];""" with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Undefined register target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[0] -> b[0];""" with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Register index out of range target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[0] -> a[2];""" with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Reuse measured qubit target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[0] -> a[0]; x q[1]; measure q[1] -> a[1];""" # Note that in this example the full register measurement is added during # the first `measurement` call with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Reuse classical register target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[0] -> a[1]; measure q[1] -> a[1];""" with pytest.raises(KeyError): c = Circuit.from_qasm(target) # Invalid measurement command target = """OPENQASM 2.0; qreg q[2]; creg a[2]; measure q[0] -> a[1] -> a[0];""" with pytest.raises(ValueError): c = Circuit.from_qasm(target)
def test_from_qasm_simple(backend, accelerators): target = f"""OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; h q[0]; h q[1]; h q[2]; h q[3]; h q[4];""" import qibo c = Circuit.from_qasm(target, accelerators) assert c.nqubits == 5 assert c.depth == 1 for i, gate in enumerate(c.queue): assert gate.__class__.__name__ == "H" assert gate.qubits == (i, ) target_state = np.ones(32) / np.sqrt(32) np.testing.assert_allclose(c(), target_state)
def test_multiqubit_gates_cirq(): c1 = Circuit(2) c1.add(gates.H(0)) c1.add(gates.CNOT(0, 1)) c1.add(gates.X(1)) c1.add(gates.SWAP(0, 1)) c1.add(gates.X(0).controlled_by(1)) 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)
def test_parametrized_gate_cirq(backend): import qibo original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(2) c1.add(gates.Y(0)) c1.add(gates.RY(1, 0.1234)) 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 np.testing.assert_allclose(final_state_c1, final_state_c2, atol=_atol) c3 = Circuit.from_qasm(c2.to_qasm()) final_state_c3 = c3() np.testing.assert_allclose(final_state_c3, final_state_c2, atol=_atol) qibo.set_backend(original_backend)
def test_toffoli_cirq(): c1 = Circuit(3) c1.add(gates.Y(0)) c1.add(gates.TOFFOLI(0, 1, 2)) c1.add(gates.X(1)) c1.add(gates.TOFFOLI(0, 2, 1)) c1.add(gates.Z(2)) c1.add(gates.TOFFOLI(1, 2, 0)) 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)
def test_from_qasm_multiple_qregs(): target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg a[2],b[1]; cx a[0],b[0]; // random comment x a[1]; qreg c[2]; // random comment 2 swap a[0],c[1]; ccx b[0],c[1],c[0];""" c = Circuit.from_qasm(target) assert c.nqubits == 5 assert c.depth == 4 assert isinstance(c.queue[0], gates.CNOT) assert c.queue[0].qubits == (0, 2) assert isinstance(c.queue[1], gates.X) assert c.queue[1].qubits == (1, ) assert isinstance(c.queue[2], gates.SWAP) assert c.queue[2].qubits == (0, 4) assert isinstance(c.queue[3], gates.TOFFOLI) assert c.queue[3].qubits == (2, 4, 3)
def test_from_qasm_multiqubit_gates(accelerators): target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; cx q[0],q[2]; x q[1]; swap q[0],q[1]; cx q[1],q[0]; ccx q[1],q[2],q[0];""" c = Circuit.from_qasm(target, accelerators) assert c.nqubits == 3 assert c.depth == 5 assert isinstance(c.queue[0], gates.CNOT) assert c.queue[0].qubits == (0, 2) assert isinstance(c.queue[1], gates.X) assert c.queue[1].qubits == (1, ) assert isinstance(c.queue[2], gates.SWAP) assert c.queue[2].qubits == (0, 1) assert isinstance(c.queue[3], gates.CNOT) assert c.queue[3].qubits == (1, 0) assert isinstance(c.queue[4], gates.TOFFOLI) assert c.queue[4].qubits == (1, 2, 0)
def test_multiqubit_gates_cirq(backend): import qibo original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(2) c1.add(gates.H(0)) c1.add(gates.CNOT(0, 1)) c1.add(gates.X(1)) c1.add(gates.SWAP(0, 1)) c1.add(gates.X(0).controlled_by(1)) 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) qibo.set_backend(original_backend)
def test_toffoli_cirq(backend): import qibo original_backend = qibo.get_backend() qibo.set_backend(backend) c1 = Circuit(3) c1.add(gates.Y(0)) c1.add(gates.TOFFOLI(0, 1, 2)) c1.add(gates.X(1)) c1.add(gates.TOFFOLI(0, 2, 1)) c1.add(gates.Z(2)) c1.add(gates.TOFFOLI(1, 2, 0)) 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) qibo.set_backend(original_backend)