• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

qiskit-community / qiskit-aqt-provider / 9255177207

27 May 2024 12:52PM UTC coverage: 99.736%. Remained the same
9255177207

push

github

web-flow
Prepare release 1.5.0 (#161)

2267 of 2273 relevant lines covered (99.74%)

3.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/examples/quickstart-estimator.py
1
# This code is part of Qiskit.
2
#
3
# (C) Copyright Alpine Quantum Technologies GmbH 2023
4
#
5
# This code is licensed under the Apache License, Version 2.0. You may
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
7
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
#
9
# Any modifications or derivative works of this code must retain this
10
# copyright notice, and modified files need to carry a notice indicating
11
# that they have been altered from the originals.
12

13
# mypy: disable-error-code="no-untyped-def"
14

15
"""Quickstart example on using the Estimator primitive.
4✔
16

17
This examples uses a variational quantum eigensolver (VQE) to find
18
the ground state energy of a Hamiltonian.
19
"""
20

21
from qiskit.circuit.library import TwoLocal
4✔
22
from qiskit.quantum_info import SparsePauliOp
4✔
23
from scipy.optimize import minimize
4✔
24

25
from qiskit_aqt_provider import AQTProvider
4✔
26
from qiskit_aqt_provider.primitives import AQTEstimator
4✔
27

28
# Select an execution backend
29
provider = AQTProvider("ACCESS_TOKEN")
4✔
30
backend = provider.get_backend("offline_simulator_no_noise")
4✔
31

32
# Instantiate an estimator on the execution backend
33
estimator = AQTEstimator(backend)
4✔
34

35
# Set the transpiler's optimization level
36
estimator.set_transpile_options(optimization_level=3)
4✔
37

38
# Specify the problem Hamiltonian
39
hamiltonian = SparsePauliOp.from_list(
4✔
40
    [
41
        ("II", -1.052373245772859),
42
        ("IZ", 0.39793742484318045),
43
        ("ZI", -0.39793742484318045),
44
        ("ZZ", -0.01128010425623538),
45
        ("XX", 0.18093119978423156),
46
    ]
47
)
48

49
# Define the VQE Ansatz, initial point, and cost function
50
ansatz = TwoLocal(num_qubits=2, rotation_blocks="ry", entanglement_blocks="cz")
4✔
51
initial_point = initial_point = [0] * 8
4✔
52

53

54
def cost_function(params, ansatz, hamiltonian, estimator):
4✔
55
    """Cost function for the VQE.
56

57
    Return the estimated expectation value of the Hamiltonian
58
    on the state prepared by the Ansatz circuit.
59
    """
60
    return estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]
4✔
61

62

63
# Run the VQE using the SciPy minimizer routine
64
result = minimize(
4✔
65
    cost_function, initial_point, args=(ansatz, hamiltonian, estimator), method="cobyla"
66
)
67

68
# Print the found minimum eigenvalue
69
print(result.fun)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc