def test_controlled_by_error(): """Check that using `to_qasm` with controlled by gates raises error.""" c = Circuit(3) c.add(gates.H(0)) c.add(gates.Y(1).controlled_by(0, 2)) with pytest.raises(ValueError): c.to_qasm()
def test_parametrized_gate(): c = Circuit(2) c.add(gates.Y(0)) c.add(gates.RY(1, 0.1234)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; y q[0]; ry(0.1234) q[1];""" assert_strings_equal(c.to_qasm(), target)
def test_simple(): c = Circuit(2) c.add(gates.H(0)) c.add(gates.H(1)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; h q[0]; h q[1];""" assert_strings_equal(c.to_qasm(), target)
def test_cu1(): c = Circuit(2) c.add(gates.RX(0, 0.1234)) c.add(gates.RZ(1, 0.4321)) c.add(gates.CU1(0, 1, 0.567)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; rx(0.1234) q[0]; rz(0.4321) q[1]; cu1(0.567) q[0],q[1];""" assert_strings_equal(c.to_qasm(), target)
def test_measurements(): c = Circuit(2) c.add(gates.X(0)) c.add(gates.Y(1)) c.add(gates.M(0, 1)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; creg register0[2]; x q[0]; y q[1]; measure q[0] -> register0[0]; measure q[1] -> register0[1];""" assert_strings_equal(c.to_qasm(), target)
def test_multiple_measurements(): c = Circuit(5) c.add(gates.M(0, 2, 4, register_name="a")) c.add(gates.M(1, 3, register_name="b")) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg a[3]; creg b[2]; measure q[0] -> a[0]; measure q[2] -> a[1]; measure q[4] -> a[2]; measure q[1] -> b[0]; measure q[3] -> b[1];""" assert_strings_equal(c.to_qasm(), target)
def test_multiqubit_gates(): c = Circuit(2) c.add(gates.H(0)) c.add(gates.CNOT(0, 1)) c.add(gates.X(1)) c.add(gates.SWAP(0, 1)) c.add(gates.X(0).controlled_by(1)) # `controlled_by` here falls back to CNOT and should work target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; h q[0]; cx q[0],q[1]; x q[1]; swap q[0],q[1]; cx q[1],q[0];""" assert_strings_equal(c.to_qasm(), target)
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_crotations(): c = Circuit(3) c.add(gates.RX(0, 0.1)) c.add(gates.RZ(1, 0.4)) c.add(gates.CRX(0, 2, 0.5)) c.add(gates.RY(1, 0.3).controlled_by(2)) 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]; crx(0.5) q[0],q[2]; cry(0.3) 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_unknown_gate_error(): """"Check that using `to_qasm` with not supported gates raises error.""" c = Circuit(2) c.add(gates.Flatten(4 * [0])) with pytest.raises(ValueError): c.to_qasm()
def test_singlequbit_gates(): c = Circuit(2) c.add(gates.H(0)) c.add(gates.X(1)) c.add(gates.Y(0)) c.add(gates.Z(1)) c.add(gates.S(0)) c.add(gates.SDG(1)) c.add(gates.T(0)) c.add(gates.TDG(1)) c.add(gates.I(0)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; h q[0]; x q[1]; y q[0]; z q[1]; s q[0]; sdg q[1]; t q[0]; tdg q[1]; id q[0];""" assert_strings_equal(c.to_qasm(), target)
def test_capital_in_register_name_error(): """Check that using capital letter in register name raises error.""" c = Circuit(2) c.add(gates.M(0, 1, register_name="Abc")) with pytest.raises(NameError): c.to_qasm()
def test_toffoli(): c = Circuit(3) c.add(gates.Y(0)) c.add(gates.TOFFOLI(0, 1, 2)) c.add(gates.X(1)) c.add(gates.TOFFOLI(0, 2, 1)) c.add(gates.Z(2)) c.add(gates.TOFFOLI(1, 2, 0)) target = f"""// Generated by QIBO {__version__} OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; y q[0]; ccx q[0],q[1],q[2]; x q[1]; ccx q[0],q[2],q[1]; z q[2]; ccx q[1],q[2],q[0];""" assert_strings_equal(c.to_qasm(), target)