def test_rotations(self): """Test that observable rotations are correctly applied.""" ops = [ qml.Hadamard(wires=0), qml.CNOT(wires=[0, 1]), qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.Hadamard(2)), ] circuit = CircuitGraph(ops, {}) res = circuit.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; creg c[3]; h q[0]; cx q[0],q[1]; h q[0]; ry(-0.7853981633974483) q[2]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; """) assert res == expected ops2 = circuit.operations + circuit.diagonalizing_gates circuit2 = CircuitGraph(ops2, {}) qasm2 = circuit2.to_openqasm() assert res == qasm2
def test_basis_state_initialization_decomposition(self): """Test that the basis state preparation decomposition is correctly applied.""" basis_state = np.array([1, 0, 1, 1]) ops1 = [qml.BasisState(basis_state, wires=[0, 1, 2, 3])] circuit1 = CircuitGraph(ops1, {}) qasm1 = circuit1.to_openqasm() ops2 = qml.BasisState.decomposition(basis_state, wires=[0, 1, 2, 3]) circuit2 = CircuitGraph(ops2, {}) qasm2 = circuit2.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[4]; creg c[4]; x q[0]; x q[2]; x q[3]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; measure q[3] -> c[3]; """) assert qasm1 == expected assert qasm1 == qasm2
def test_state_initialization_decomposition(self): """Test that the Mottonen state prepration decomposition is correctly applied.""" psi = np.array([1, -1, -1, 1]) / np.sqrt(4) ops1 = [qml.QubitStateVector(psi, wires=[0, 1])] circuit1 = CircuitGraph(ops1, {}) qasm1 = circuit1.to_openqasm() ops2 = qml.QubitStateVector.decomposition(psi, wires=[0, 1]) circuit2 = CircuitGraph(ops2, {}) qasm2 = circuit2.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; creg c[2]; ry(1.5707963267948968) q[1]; ry(1.5707963267948963) q[0]; cx q[1],q[0]; ry(0.0) q[0]; cx q[1],q[0]; rz(0.0) q[0]; cx q[1],q[0]; rz(3.141592653589793) q[0]; cx q[1],q[0]; measure q[0] -> c[0]; measure q[1] -> c[1]; """) assert qasm1 == expected assert qasm1 == qasm2
def test_rotation_gate_decomposition(self): """Test that gates not natively supported by QASM, such as the rotation gate, are correctly decomposed and serialized.""" ops1 = [qml.Rot(0.3, 0.1, 0.2, wires=1)] circuit1 = CircuitGraph(ops1, {}) qasm1 = circuit1.to_openqasm() ops2 = qml.Rot.decomposition(0.3, 0.1, 0.2, wires=1) circuit2 = CircuitGraph(ops2, {}) qasm2 = circuit2.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; creg c[2]; rz(0.3) q[1]; ry(0.1) q[1]; rz(0.2) q[1]; measure q[0] -> c[0]; measure q[1] -> c[1]; """) assert qasm1 == expected assert qasm1 == qasm2
def test_unused_wires(self): """Test that unused wires are correctly taken into account""" ops = [ qml.Hadamard(wires=4), qml.CNOT(wires=[1, 0]), ] circuit = CircuitGraph(ops, {}) res = circuit.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg c[5]; h q[4]; cx q[1],q[0]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; measure q[3] -> c[3]; measure q[4] -> c[4]; """) assert res == expected
def test_native_inverse_gates(self): """Test that a circuit containing inverse gates that are supported natively by QASM, such as sdg, are correctly serialized.""" ops = [ qml.S(wires=0), qml.S(wires=0).inv(), qml.T(wires=0), qml.T(wires=0).inv(), ] circuit = CircuitGraph(ops, {}) res = circuit.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[1]; creg c[1]; s q[0]; sdg q[0]; t q[0]; tdg q[0]; measure q[0] -> c[0]; """) assert res == expected
def test_native_qasm_gates(self): """Test that a circuit containing solely native QASM gates is properly serialized.""" ops = [ qml.RX(0.43, wires=0), qml.RY(0.35, wires=1), qml.RZ(0.35, wires=2), qml.CNOT(wires=[0, 1]), qml.Hadamard(wires=2), qml.CNOT(wires=[2, 0]), qml.PauliX(wires=1), ] circuit = CircuitGraph(ops, {}) res = circuit.to_openqasm() expected = dedent("""\ OPENQASM 2.0; include "qelib1.inc"; qreg q[3]; creg c[3]; rx(0.43) q[0]; ry(0.35) q[1]; rz(0.35) q[2]; cx q[0],q[1]; h q[2]; cx q[2],q[0]; x q[1]; measure q[0] -> c[0]; measure q[1] -> c[1]; measure q[2] -> c[2]; """) assert res == expected
def test_empty_circuit(self): """Test that an empty circuit graph is properly serialized into an empty QASM program.""" circuit = CircuitGraph([], {}) res = circuit.to_openqasm() expected = 'OPENQASM 2.0;\ninclude "qelib1.inc";\n' assert res == expected
def test_unsupported_gate(self): """Test an exception is raised if an unsupported operation is applied.""" U = np.array([[1, 1], [1, -1]]) / np.sqrt(2) ops = [qml.S(wires=0), qml.QubitUnitary(U, wires=[0, 1])] circuit = CircuitGraph(ops, {}, Wires([0, 1])) with pytest.raises( ValueError, match="QubitUnitary not supported by the QASM serializer"): res = circuit.to_openqasm()