def test_multi_swaps(self): """Test that transform works with several SWAPs.""" def qfunc1(): qml.Hadamard(wires=0) qml.PauliX(wires=1) qml.SWAP(wires=[0, 1]) qml.SWAP(wires=[0, 2]) qml.PauliY(wires=0) return qml.expval(qml.PauliZ(0)) def qfunc2(): qml.Hadamard(wires=1) qml.PauliX(wires=2) qml.PauliY(wires=0) return qml.expval(qml.PauliZ(0)) transformed_qfunc = undo_swaps(qfunc1) tape1 = qml.transforms.make_tape(transformed_qfunc)() res1 = tape1.execute(qml.device("default.qubit", wires=3)) tape2 = qml.transforms.make_tape(qfunc2)() res2 = tape2.execute(qml.device("default.qubit", wires=3)) assert np.allclose(res1, res2)
def test_templates_transform(self): """Test that a template changes correctly with a SWAP.""" def qfunc1(): qml.RX(2, wires=0) qml.RY(-3, wires=1) qml.QFT(wires=[0, 1, 2]) qml.SWAP(wires=[1, 2]) return qml.state() def qfunc2(): qml.RX(2, wires=0) qml.RY(-3, wires=2) qml.QFT(wires=[0, 2, 1]) return qml.state() transformed_qfunc = undo_swaps(qfunc1) tape1 = qml.transforms.make_tape(transformed_qfunc)() res1 = tape1.execute(qml.device("default.qubit", wires=3)) tape2 = qml.transforms.make_tape(qfunc2)() res2 = tape2.execute(qml.device("default.qubit", wires=3)) assert np.allclose(res1, res2)
def test_one_qubit_gates_transform(self): """Test that a single-qubit gate changes correctly with a SWAP.""" def qfunc(): qml.Hadamard(wires=0) qml.PauliX(wires=1) qml.SWAP(wires=[0, 1]) return qml.probs(1) transformed_qfunc = undo_swaps(qfunc) tape = qml.transforms.make_tape(transformed_qfunc)() res = tape.execute(qml.device("default.qubit", wires=2)) assert len(tape.operations) == 2 assert np.allclose(res[0][0], 0.5)
def test_two_qubits_gates_transform(self): """Test that a two-qubit gate changes correctly with a SWAP.""" def qfunc(): qml.PauliX(wires=1) qml.CNOT(wires=[0, 1]) qml.SWAP(wires=[0, 1]) return qml.state() transformed_qfunc = undo_swaps(qfunc) tape = qml.transforms.make_tape(transformed_qfunc)() res = tape.execute(qml.device("default.qubit", wires=2)) assert len(tape.operations) == 2 assert np.allclose(res[0][2], 1.0)
# Example QNode and device for interface testing dev = qml.device("default.qubit", wires=3) # Test each of single-qubit, two-qubit, and Rot gates def qfunc(theta): qml.Hadamard(wires=0) qml.RX(theta[0], wires=1) qml.SWAP(wires=[0, 1]) qml.SWAP(wires=[0, 2]) qml.RY(theta[1], wires=0) return qml.expval(qml.PauliZ(0)) transformed_qfunc = undo_swaps(qfunc) expected_op_list = ["Hadamard", "RX", "RY"] expected_wires_list = [ Wires(1), Wires(2), Wires(0), ] class TestUndoSwapsInterfaces: """Test that `undo_swaps` transform works in all interfaces.""" def test_undo_swaps_autograd(self): """Test QNode and gradient in autograd interface."""