示例#1
0
    def test_invalid_ansatz(self, token, tol, shots):
        """Test that an invalid ansatz cannot be passed."""
        IBMQ.enable_account(token)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(ValueError, match="Input ansatz is not a quantum function or a string."):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=10,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer_config={"maxiter": 10},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#2
0
    def test_not_auth_operation_hamiltonian_tensor(self, token, tol, shots):
        """Test the observables in the tensor Hamiltonian are I, X, Y, Z or H."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RX(params[1], wires=1)

        H = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])
        coeffs = [1, 1]
        obs = [qml.PauliX(0) @ qml.Hermitian(H, wires=1), qml.PauliZ(wires=1)]
        hamiltonian = qml.Hamiltonian(coeffs, obs)

        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(qml.QuantumFunctionError, match="Observable is not accepted."):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer_config={"maxiter": 10},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#3
0
    def test_invalid_function(self, token, tol, shots):
        """Test that an invalid function cannot be passed."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            c = params[0] + params[1]

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(
            qml.QuantumFunctionError, match="Function contains no quantum operations."
        ):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer_config={"maxiter": 10},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#4
0
    def test_hamiltonian_format(self, token, tol, shots):
        """Test that a PennyLane Hamiltonian is required."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RX(params[1], wires=1)

        hamiltonian = qml.PauliZ(wires=0)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(
            qml.QuantumFunctionError, match="A PennyLane Hamiltonian object is required."
        ):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer_config={"maxiter": 10},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#5
0
    def test_wrong_number_input_param(self, token, tol, shots):
        """Test that we need a certain number of parameters."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RY(params[1], wires=0)
            qml.RY(params[2], wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(qml.QuantumFunctionError, match="Not enough parameters in X0."):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[0, 0],
                shots=shots,
                optimizer="SPSA",
                optimizer_config={"maxiter": 40},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#6
0
    def test_wrong_input(self, token, tol, shots):
        """Test that we can only give a single vector parameter to the ansatz circuit."""
        IBMQ.enable_account(token)

        def vqe_circuit(params, wire):
            qml.RX(params[0], wires=wire)
            qml.RY(params[1], wires=wire)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(qml.QuantumFunctionError, match="Param should be a single vector."):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer="SPSA",
                optimizer_config={"maxiter": 40},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#7
0
    def test_qnode(self, token, tol, shots):
        """Test that we cannot pass a QNode as ansatz circuit."""
        IBMQ.enable_account(token)

        with qml.tape.QuantumTape() as vqe_tape:
            qml.RX(3.97507603, wires=0)
            qml.RY(3.00854038, wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(
            qml.QuantumFunctionError, match="The ansatz must be a callable quantum function."
        ):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_tape,
                x0=[3.97507603, 3.00854038],
                shots=shots,
                optimizer="SPSA",
                optimizer_config={"maxiter": 40},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#8
0
    def test_ansatz_qiskit_invalid(self, token, tol, shots):
        """Test a simple VQE problem with an invalid ansatz from Qiskit library."""
        IBMQ.enable_account(token)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.raises(
            ValueError, match="Ansatz InEfficientSU2 not in n_local circuit library."
        ):
            vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz="InEfficientSU2",
                x0=[3.97507603, 3.00854038],
                shots=shots,
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
示例#9
0
    def test_scipy_optimizer(self, token, tol, shots):
        """Test we can run a VQE problem with a SciPy optimizer."""
        IBMQ.enable_account(token)
        tol = 1e-1

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RY(params[1], wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        job = vqe_runner(
            program_id=program_id,
            backend="ibmq_qasm_simulator",
            hamiltonian=hamiltonian,
            ansatz=vqe_circuit,
            x0=[3.97507603, 3.00854038],
            shots=shots,
            optimizer="Powell",
            optimizer_config={"maxiter": 10},
            kwargs={"hub": "ibm-q-startup", "group": "ibm-q-startup", "project": "reservations"},
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)
        result = job.result()["fun"]

        assert np.allclose(result, -1.43, tol)
        assert "parameters" in job.intermediate_results
示例#10
0
    def test_hamiltonian_tensor(self, token, tol, shots):
        """Test that we can handle tensor Hamiltonians."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RX(params[1], wires=1)

        coeffs = [0.2, -0.543]
        obs = [qml.PauliX(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.Hadamard(1)]
        hamiltonian = qml.Hamiltonian(coeffs, obs)

        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        job = vqe_runner(
            program_id=program_id,
            backend="ibmq_qasm_simulator",
            hamiltonian=hamiltonian,
            ansatz=vqe_circuit,
            x0=[3.97507603, 3.00854038],
            shots=shots,
            optimizer_config={"maxiter": 10},
            kwargs={"hub": "ibm-q-startup", "group": "ibm-q-startup", "project": "reservations"},
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)

        assert isinstance(job.intermediate_results, dict)
        assert "nfev" in job.intermediate_results
        assert "parameters" in job.intermediate_results
        assert "function" in job.intermediate_results
        assert "step" in job.intermediate_results
        assert "accepted" in job.intermediate_results
示例#11
0
    def test_one_param(self, token, tol, shots):
        """Test that we can only give a single vector parameter to the ansatz circuit."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params, wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        job = vqe_runner(
            program_id=program_id,
            backend="ibmq_qasm_simulator",
            hamiltonian=hamiltonian,
            ansatz=vqe_circuit,
            x0=[0.0],
            shots=shots,
            optimizer_config={"maxiter": 10},
            kwargs={"hub": "ibm-q-startup", "group": "ibm-q-startup", "project": "reservations"},
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)

        assert isinstance(job.intermediate_results, dict)
        assert "nfev" in job.intermediate_results
        assert "parameters" in job.intermediate_results
        assert "function" in job.intermediate_results
        assert "step" in job.intermediate_results
        assert "accepted" in job.intermediate_results
示例#12
0
    def test_ansatz_qiskit(self, token, tol, shots):
        """Test a simple VQE problem with an ansatz from Qiskit library."""
        IBMQ.enable_account(token)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        job = vqe_runner(
            program_id=program_id,
            backend="ibmq_qasm_simulator",
            hamiltonian=hamiltonian,
            ansatz="EfficientSU2",
            x0=[0.0, 0.0, 0.0, 0.0],
            shots=shots,
            kwargs={"hub": "ibm-q-startup", "group": "ibm-q-startup", "project": "reservations"},
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)

        assert isinstance(job.intermediate_results, dict)
        assert "nfev" in job.intermediate_results
        assert "parameters" in job.intermediate_results
        assert "function" in job.intermediate_results
        assert "step" in job.intermediate_results
        assert "accepted" in job.intermediate_results
示例#13
0
    def test_too_many_param(self, token, tol, shots):
        """Test that we handle the case where too many parameters were given."""
        IBMQ.enable_account(token)

        def vqe_circuit(params):
            qml.RX(params[0], wires=0)
            qml.RY(params[1], wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        with pytest.warns(UserWarning) as record:
            job = vqe_runner(
                program_id=program_id,
                backend="ibmq_qasm_simulator",
                hamiltonian=hamiltonian,
                ansatz=vqe_circuit,
                x0=[3.97507603, 3.00854038, 3.55637849],
                shots=shots,
                optimizer_config={"maxiter": 10},
                kwargs={
                    "hub": "ibm-q-startup",
                    "group": "ibm-q-startup",
                    "project": "reservations",
                },
            )

        assert (
            record[-1].message.args[0]
            == "In order to match the tape expansion, the number of parameters has been changed."
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)

        assert isinstance(job.intermediate_results, dict)
        assert "nfev" in job.intermediate_results
        assert "parameters" in job.intermediate_results
        assert "function" in job.intermediate_results
        assert "step" in job.intermediate_results
        assert "accepted" in job.intermediate_results
示例#14
0
    def test_simple_hamiltonian_with_untrainable_parameters(self, token, tol, shots):
        """Test a simple VQE problem with untrainable parameters."""
        IBMQ.enable_account(token)
        tol = 1e-1

        def vqe_circuit(params):
            qml.RZ(0.1, wires=0)
            qml.RX(params[0], wires=0)
            qml.RY(params[1], wires=0)
            qml.RZ(0.2, wires=0)

        coeffs = [1, 1]
        obs = [qml.PauliX(0), qml.PauliZ(0)]

        hamiltonian = qml.Hamiltonian(coeffs, obs)
        program_id = upload_vqe_runner(hub="ibm-q-startup", group="xanadu", project="reservations")

        job = vqe_runner(
            program_id=program_id,
            backend="ibmq_qasm_simulator",
            hamiltonian=hamiltonian,
            ansatz=vqe_circuit,
            x0=[3.97507603, 3.00854038],
            shots=shots,
            optimizer="SPSA",
            optimizer_config={"maxiter": 40},
            kwargs={"hub": "ibm-q-startup", "group": "ibm-q-startup", "project": "reservations"},
        )

        provider = IBMQ.get_provider(hub="ibm-q-startup", group="xanadu", project="reservations")
        delete_vqe_runner(provider=provider, program_id=program_id)

        assert np.allclose(job.result()["fun"], -1.43, tol)
        assert isinstance(job.intermediate_results, dict)
        assert "nfev" in job.intermediate_results
        assert "parameters" in job.intermediate_results
        assert "function" in job.intermediate_results
        assert "step" in job.intermediate_results
        assert "accepted" in job.intermediate_results