def test_from_qasm_invalid_parametrized_gates(): # Parametrize non-parametrized gate target = """OPENQASM 2.0; qreg q[2]; x(0.1234) q[0]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Failure to give theta value for parametrized gate target = """OPENQASM 2.0; qreg q[2]; rx q[0]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Invalid parameter value target = """OPENQASM 2.0; qreg q[2]; rx(0.123a) q[0]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Invalid parenthesis use target = """OPENQASM 2.0; qreg q[2]; rx(0.123)(0.25)(0) q[0]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target)
def test_from_qasm_empty_spaces(backend): target = """OPENQASM 2.0; qreg q[2]; creg a[2]; h q[0];x q[1]; cx q[0], q[1]; measure q[0] -> a[0];measure q[1]->a[1]""" c = Circuit.from_qasm(target) assert c.nqubits == 2 assert c.depth == 2 assert isinstance(c.queue[0], gates.H) assert isinstance(c.queue[1], gates.X) assert isinstance(c.queue[2], gates.CNOT) assert c.measurement_tuples == {"a": (0, 1)}
def test_from_qasm_parametrized_gates(): 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) assert c.queue[0].parameters == 0.1234 assert c.queue[1].parameters == 0.4321 assert c.queue[2].parameters == 0.567
def test_from_qasm_measurements_order(backend): target = """OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg a[3]; creg b[2]; measure q[2] -> b[1]; measure q[3] -> a[1]; measure q[4] -> a[0]; measure q[1] -> a[2]; measure q[0] -> b[0]; """ c = Circuit.from_qasm(target) assert c.measurement_tuples == {"a": (4, 3, 1), "b": (0, 2)}
def test_from_qasm_crotations(): target = """OPENQASM 2.0; qreg q[2]; crx(0.1) q[0],q[1]; crz(0.3) q[1],q[0]; cry(0.2) q[0],q[1];""" c = Circuit.from_qasm(target) assert c.depth == 3 assert isinstance(c.queue[0], gates.CRX) assert isinstance(c.queue[1], gates.CRZ) assert isinstance(c.queue[2], gates.CRY) assert c.queue[0].parameters == 0.1 assert c.queue[1].parameters == 0.3 assert c.queue[2].parameters == 0.2
def test_from_qasm_ugates(): 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) assert c.queue[0].parameters == 0.1 assert c.queue[1].parameters == (0.2, 0.6) assert c.queue[2].parameters == (0.3, 0.4, 0.5)
def test_from_qasm_invalid_script(): # Missing starting line target = """qreg q[2]; x q[1];""" with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Qubit index out of range target = """OPENQASM 2.0; qreg q[2]; x q[2]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Invalid qubit index target = """OPENQASM 2.0; qreg q[a]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Undefined qubit target = """OPENQASM 2.0; qreg q[2]; x a[0]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target) # Invalid command `test` target = """OPENQASM 2.0; test q[2]; """ with pytest.raises(ValueError): c = Circuit.from_qasm(target)
def test_from_qasm_measurements(backend): 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_invalid_measurements(backend): # 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_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 == 3 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_singlequbit_gates(): 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];""" c = Circuit.from_qasm(target) assert c.nqubits == 2 assert c.depth == 5 assert c.ngates == 9 assert isinstance(c.queue[0], gates.H) assert c.queue[0].qubits == (0,) assert isinstance(c.queue[1], gates.X) assert c.queue[1].qubits == (1,) assert isinstance(c.queue[2], gates.Y) assert c.queue[2].qubits == (0,) assert isinstance(c.queue[3], gates.Z) assert c.queue[3].qubits == (1,) assert isinstance(c.queue[4], gates.S) assert c.queue[4].qubits == (0,) assert isinstance(c.queue[5], gates.SDG) assert c.queue[5].qubits == (1,) assert isinstance(c.queue[6], gates.T) assert c.queue[6].qubits == (0,) assert isinstance(c.queue[7], gates.TDG) assert c.queue[7].qubits == (1,) assert isinstance(c.queue[8], gates.I) assert c.queue[8].qubits == (0,)
def test_from_qasm_multiqubit_gates(): 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) assert c.nqubits == 3 assert c.depth == 4 assert c.ngates == 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)