• 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/qaoa.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
"""Trivial minimization example using a quantum approximate optimization algorithm (QAOA).
4✔
14

15
This is the same example as in vqe.py, but uses QAOA instead of VQE as solver.
16
"""
17

18
from typing import Final
4✔
19

20
import qiskit_algorithms
4✔
21
from qiskit.quantum_info import SparsePauliOp
4✔
22
from qiskit_algorithms.minimum_eigensolvers import QAOA
4✔
23
from qiskit_algorithms.optimizers import COBYLA
4✔
24

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

28
RANDOM_SEED: Final = 0
4✔
29

30
if __name__ == "__main__":
4✔
31
    backend = AQTProvider("token").get_backend("offline_simulator_no_noise")
4✔
32
    sampler = AQTSampler(backend)
4✔
33

34
    # fix the random seeds such that the example is reproducible
35
    qiskit_algorithms.utils.algorithm_globals.random_seed = RANDOM_SEED
4✔
36
    backend.simulator.options.seed_simulator = RANDOM_SEED
4✔
37

38
    # Hamiltonian: Ising model on two spin 1/2 without external field
39
    J = 1.23456789
4✔
40
    hamiltonian = SparsePauliOp.from_list([("ZZ", 3 * J)])
4✔
41

42
    # Find the ground-state energy with QAOA
43
    optimizer = COBYLA(maxiter=100, tol=0.01)
4✔
44
    qaoa = QAOA(sampler, optimizer)
4✔
45
    result = qaoa.compute_minimum_eigenvalue(operator=hamiltonian)
4✔
46
    assert result.eigenvalue is not None  # noqa: S101
4✔
47

48
    print(f"Optimizer run time: {result.optimizer_time:.2f} s")
4✔
49
    print("Cost function evaluations:", result.cost_function_evals)
4✔
50
    print("Deviation from expected ground-state energy:", abs(result.eigenvalue - (-3 * J)))
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