Ejemplo n.º 1
0
    def test_invalid_target(self, interface):
        with pytest.raises(ValueError, match="Input target of incorrect length 2 instead of 1"):
            num_qubits = 1

            dev = qml.device("default.qubit", wires=num_qubits)
            observables = [qml.PauliZ(0)]
            loss = SquaredErrorLoss(rx_ansatz, observables, dev, interface=interface)

            phis = np.ones(num_qubits)
            loss(phis, target=np.array([1.0, 2.0]))
Ejemplo n.º 2
0
    def test_no_target(self, interface):
        with pytest.raises(ValueError, match="The target cannot be None"):
            num_qubits = 1

            dev = qml.device("default.qubit", wires=num_qubits)
            observables = [qml.PauliZ(0)]
            loss = SquaredErrorLoss(rx_ansatz, observables, dev, interface=interface)

            phis = np.ones(num_qubits)
            loss(phis)
Ejemplo n.º 3
0
def test_deprecation_warning():
    """Test if deprecation warning is raised"""
    if int(qml.__version__.split(".")[1]) >= 16:
        pytest.fail(
            "Deprecation warnings for the qnn module should be removed")

    dev = qml.device("default.qubit", wires=3)
    observables = [qml.PauliZ(0), qml.PauliX(0), qml.PauliZ(1) @ qml.PauliZ(2)]

    with pytest.warns(DeprecationWarning, match=WARNING_STRING):
        SquaredErrorLoss(rx_ansatz, observables, dev)
Ejemplo n.º 4
0
    def test_rx_circuit(self, interface):
        num_qubits = 3

        dev = qml.device("default.qubit", wires=num_qubits)
        observables = [qml.PauliZ(0), qml.PauliX(0), qml.PauliZ(1) @ qml.PauliZ(2)]
        loss = SquaredErrorLoss(rx_ansatz, observables, dev, interface=interface)

        phis = np.ones(num_qubits)
        res = loss(phis, target=np.array([1.0, 0.5, 0.1]))

        assert np.allclose(res, np.array([0.21, 0.25, 0.03]), atol=0.01, rtol=0.01)
Ejemplo n.º 5
0
    def test_layer_circuit(self, interface):
        num_qubits = 3

        dev = qml.device("default.qubit", wires=num_qubits)
        observables = [qml.PauliZ(0), qml.PauliX(0), qml.PauliZ(1) @ qml.PauliZ(2)]
        loss = SquaredErrorLoss(layer_ansatz, observables, dev, interface=interface)

        weights = np.ones((num_qubits, 3, 3))
        res = loss(weights, x=np.array([1.0, 2.0, 1.0]), target=np.array([1.0, 0.5, 0.1]))

        assert np.allclose(res, np.array([0.88, 0.83, 0.05]), atol=0.01, rtol=0.01)