示例#1
0
    def test_operation_list_as_position(self):
        """Test if expected tape is returned when an operation list is passed in position"""
        tape = insert(qml.PhaseDamping, 0.4, position=[qml.RX, qml.RY])(self.tape)

        with QuantumTape() as tape_exp:
            qml.RX(0.9, wires=0)
            qml.PhaseDamping(0.4, wires=0)
            qml.RY(0.4, wires=1)
            qml.PhaseDamping(0.4, wires=1)
            qml.CNOT(wires=[0, 1])
            qml.RY(0.5, wires=0)
            qml.PhaseDamping(0.4, wires=0)
            qml.RX(0.6, wires=1)
            qml.PhaseDamping(0.4, wires=1)
            qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

        assert all(o1.name == o2.name for o1, o2 in zip(tape.operations, tape_exp.operations))
        assert all(o1.wires == o2.wires for o1, o2 in zip(tape.operations, tape_exp.operations))
        assert all(
            np.allclose(o1.parameters, o2.parameters)
            for o1, o2 in zip(tape.operations, tape_exp.operations)
        )
        assert len(tape.measurements) == 1
        assert tape.observables[0].name == ["PauliZ", "PauliZ"]
        assert tape.observables[0].wires.tolist() == [0, 1]
        assert tape.measurements[0].return_type is Expectation
示例#2
0
    def test_all_with_state_prep(self):
        """Test if the expected tape is returned when the all position is requested in a tape
        that has state preparation"""
        tape = insert(qml.PhaseDamping, 0.4,
                      position="all")(self.tape_with_prep)

        with QuantumTape() as tape_exp:
            qml.QubitStateVector([1, 0], wires=0)
            qml.RX(0.9, wires=0)
            qml.PhaseDamping(0.4, wires=0)
            qml.RY(0.4, wires=1)
            qml.PhaseDamping(0.4, wires=1)
            qml.CNOT(wires=[0, 1])
            qml.PhaseDamping(0.4, wires=0)
            qml.PhaseDamping(0.4, wires=1)
            qml.RY(0.5, wires=0)
            qml.PhaseDamping(0.4, wires=0)
            qml.RX(0.6, wires=1)
            qml.PhaseDamping(0.4, wires=1)
            qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

        assert all(o1.name == o2.name
                   for o1, o2 in zip(tape.operations, tape_exp.operations))
        assert all(o1.wires == o2.wires
                   for o1, o2 in zip(tape.operations, tape_exp.operations))
        assert all(
            np.allclose(o1.parameters, o2.parameters)
            for o1, o2 in zip(tape.operations, tape_exp.operations))
        assert len(tape.measurements) == 1
        assert tape.observables[0].name == ["PauliZ", "PauliZ"]
        assert tape.observables[0].wires.tolist() == [0, 1]
        assert tape.measurements[0].return_type is Expectation
示例#3
0
def test_insert_dev(mocker, monkeypatch):
    """Test if a device transformed by the insert function does successfully add noise to
    subsequent circuit executions"""
    with QuantumTape() as in_tape:
        qml.RX(0.9, wires=0)
        qml.RY(0.4, wires=1)
        qml.CNOT(wires=[0, 1])
        qml.RY(0.5, wires=0)
        qml.RX(0.6, wires=1)
        qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

    dev = qml.device("default.mixed", wires=2)
    res_without_noise = qml.execute([in_tape], dev, qml.gradients.param_shift)

    new_dev = insert(qml.PhaseDamping, 0.4)(dev)
    spy = mocker.spy(new_dev, "default_expand_fn")

    res_with_noise = qml.execute([in_tape], new_dev, qml.gradients.param_shift)
    tape = spy.call_args[0][0]

    with QuantumTape() as tape_exp:
        qml.RX(0.9, wires=0)
        qml.PhaseDamping(0.4, wires=0)
        qml.RY(0.4, wires=1)
        qml.PhaseDamping(0.4, wires=1)
        qml.CNOT(wires=[0, 1])
        qml.PhaseDamping(0.4, wires=0)
        qml.PhaseDamping(0.4, wires=1)
        qml.RY(0.5, wires=0)
        qml.PhaseDamping(0.4, wires=0)
        qml.RX(0.6, wires=1)
        qml.PhaseDamping(0.4, wires=1)
        qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

    assert all(o1.name == o2.name
               for o1, o2 in zip(tape.operations, tape_exp.operations))
    assert all(o1.wires == o2.wires
               for o1, o2 in zip(tape.operations, tape_exp.operations))
    assert all(
        np.allclose(o1.parameters, o2.parameters)
        for o1, o2 in zip(tape.operations, tape_exp.operations))
    assert len(tape.measurements) == 1
    assert tape.observables[0].name == ["PauliZ", "PauliZ"]
    assert tape.observables[0].wires.tolist() == [0, 1]
    assert tape.measurements[0].return_type is Expectation

    assert not np.allclose(res_without_noise, res_with_noise)