amazon-braket-algorithm-library 1.7.10__tar.gz → 1.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {amazon_braket_algorithm_library-1.7.10/src/amazon_braket_algorithm_library.egg-info → amazon_braket_algorithm_library-1.8.0}/PKG-INFO +2 -1
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/README.md +1 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0/src/amazon_braket_algorithm_library.egg-info}/PKG-INFO +2 -1
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/amazon_braket_algorithm_library.egg-info/SOURCES.txt +3 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/_algos/_version.py +1 -1
- amazon_braket_algorithm_library-1.8.0/src/braket/experimental/algorithms/variational_quantum_eigensolver/__init__.py +28 -0
- amazon_braket_algorithm_library-1.8.0/src/braket/experimental/algorithms/variational_quantum_eigensolver/variational_quantum_eigensolver.md +7 -0
- amazon_braket_algorithm_library-1.8.0/src/braket/experimental/algorithms/variational_quantum_eigensolver/variational_quantum_eigensolver.py +486 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/LICENSE +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/NOTICE +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/pyproject.toml +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/setup.cfg +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/amazon_braket_algorithm_library.egg-info/dependency_links.txt +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/amazon_braket_algorithm_library.egg-info/requires.txt +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/amazon_braket_algorithm_library.egg-info/top_level.txt +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/adaptive_shot_allocation/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/adaptive_shot_allocation/adaptive_allocator.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/adaptive_shot_allocation/adaptive_allocator_braket_helpers.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/adaptive_shot_allocation/adaptive_shot_allocation.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bells_inequality/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bells_inequality/bells_inequality.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bells_inequality/bells_inequality.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bernstein_vazirani/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bernstein_vazirani/bernstein_vazirani.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/bernstein_vazirani/bernstein_vazirani.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/chsh_inequality/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/chsh_inequality/chsh_inequality.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/chsh_inequality/chsh_inequality.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/deutsch_jozsa/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/deutsch_jozsa/deutsch_jozsa.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/deutsch_jozsa/deutsch_jozsa.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/grovers_search/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/grovers_search/grovers_search.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/grovers_search/grovers_search.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/hhl/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/hhl/hhl.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/hhl/hhl.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/qc_qmc/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/qc_qmc/classical_qmc.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/qc_qmc/qc_qmc.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_approximate_optimization/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_approximate_optimization/quantum_approximate_optimization.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_approximate_optimization/quantum_approximate_optimization.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_circuit_born_machine/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_circuit_born_machine/qcbm.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_circuit_born_machine/quantum_circuit_born_machine.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_counting/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_counting/quantum_counting.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_counting/quantum_counting.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_fourier_transform/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_fourier_transform/quantum_fourier_transform.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_fourier_transform/quantum_fourier_transform.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_phase_estimation/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_phase_estimation/quantum_phase_estimation.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_phase_estimation/quantum_phase_estimation.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_walk/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_walk/quantum_walk.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/quantum_walk/quantum_walk.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/rabi_oscillations/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/rabi_oscillations/rabi_oscillations.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/rabi_oscillations/rabi_oscillations.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/shors/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/shors/shors.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/shors/shors.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/simons/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/simons/simons.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/algorithms/simons/simons.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/auxiliary_functions/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/auxiliary_functions/random_circuit/__init__.py +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/auxiliary_functions/random_circuit/random_circuit.md +0 -0
- {amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/src/braket/experimental/auxiliary_functions/random_circuit/random_circuit.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: amazon-braket-algorithm-library
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.8.0
|
|
4
4
|
Summary: An open source library of quantum computing algorithms implemented on Amazon Braket
|
|
5
5
|
Author: Amazon Web Services
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -72,6 +72,7 @@ Running notebooks locally requires additional dependencies located in [notebooks
|
|
|
72
72
|
| QMC | [Quantum_Computing_Quantum_Monte_Carlo.ipynb](notebooks/advanced_algorithms/Quantum_Computing_Quantum_Monte_Carlo.ipynb) | [Motta2018](https://wires.onlinelibrary.wiley.com/doi/10.1002/wcms.1364), [Peruzzo2014](https://www.nature.com/articles/ncomms5213) |
|
|
73
73
|
| Adaptive Shot Allocation | [2_Adaptive_Shot_Allocation.ipynb](notebooks/advanced_algorithms/adaptive_shot_allocation/2_Adaptive_Shot_Allocation.ipynb) | [Shlosberg2023](https://doi.org/10.22331/q-2023-01-26-906) |
|
|
74
74
|
| HHL Algorithm | [HHL_Algorithm.ipynb](notebooks/advanced_algorithms/HHL_Algorithm.ipynb) | [Harrow2009](https://arxiv.org/abs/0811.3171) |
|
|
75
|
+
| Variational Quantum Eigensolver | [Variational_Quantum_Eigensolver.ipynb](notebooks/advanced_algorithms/Variational_Quantum_Eigensolver.ipynb) | [Peruzzo2014](https://www.nature.com/articles/ncomms5213), [McClean2016](https://iopscience.iop.org/article/10.1088/1367-2630/18/2/023023) |
|
|
75
76
|
|
|
76
77
|
|
|
77
78
|
| Auxiliary functions | Notebook |
|
|
@@ -35,6 +35,7 @@ Running notebooks locally requires additional dependencies located in [notebooks
|
|
|
35
35
|
| QMC | [Quantum_Computing_Quantum_Monte_Carlo.ipynb](notebooks/advanced_algorithms/Quantum_Computing_Quantum_Monte_Carlo.ipynb) | [Motta2018](https://wires.onlinelibrary.wiley.com/doi/10.1002/wcms.1364), [Peruzzo2014](https://www.nature.com/articles/ncomms5213) |
|
|
36
36
|
| Adaptive Shot Allocation | [2_Adaptive_Shot_Allocation.ipynb](notebooks/advanced_algorithms/adaptive_shot_allocation/2_Adaptive_Shot_Allocation.ipynb) | [Shlosberg2023](https://doi.org/10.22331/q-2023-01-26-906) |
|
|
37
37
|
| HHL Algorithm | [HHL_Algorithm.ipynb](notebooks/advanced_algorithms/HHL_Algorithm.ipynb) | [Harrow2009](https://arxiv.org/abs/0811.3171) |
|
|
38
|
+
| Variational Quantum Eigensolver | [Variational_Quantum_Eigensolver.ipynb](notebooks/advanced_algorithms/Variational_Quantum_Eigensolver.ipynb) | [Peruzzo2014](https://www.nature.com/articles/ncomms5213), [McClean2016](https://iopscience.iop.org/article/10.1088/1367-2630/18/2/023023) |
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
| Auxiliary functions | Notebook |
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: amazon-braket-algorithm-library
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.8.0
|
|
4
4
|
Summary: An open source library of quantum computing algorithms implemented on Amazon Braket
|
|
5
5
|
Author: Amazon Web Services
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -72,6 +72,7 @@ Running notebooks locally requires additional dependencies located in [notebooks
|
|
|
72
72
|
| QMC | [Quantum_Computing_Quantum_Monte_Carlo.ipynb](notebooks/advanced_algorithms/Quantum_Computing_Quantum_Monte_Carlo.ipynb) | [Motta2018](https://wires.onlinelibrary.wiley.com/doi/10.1002/wcms.1364), [Peruzzo2014](https://www.nature.com/articles/ncomms5213) |
|
|
73
73
|
| Adaptive Shot Allocation | [2_Adaptive_Shot_Allocation.ipynb](notebooks/advanced_algorithms/adaptive_shot_allocation/2_Adaptive_Shot_Allocation.ipynb) | [Shlosberg2023](https://doi.org/10.22331/q-2023-01-26-906) |
|
|
74
74
|
| HHL Algorithm | [HHL_Algorithm.ipynb](notebooks/advanced_algorithms/HHL_Algorithm.ipynb) | [Harrow2009](https://arxiv.org/abs/0811.3171) |
|
|
75
|
+
| Variational Quantum Eigensolver | [Variational_Quantum_Eigensolver.ipynb](notebooks/advanced_algorithms/Variational_Quantum_Eigensolver.ipynb) | [Peruzzo2014](https://www.nature.com/articles/ncomms5213), [McClean2016](https://iopscience.iop.org/article/10.1088/1367-2630/18/2/023023) |
|
|
75
76
|
|
|
76
77
|
|
|
77
78
|
| Auxiliary functions | Notebook |
|
|
@@ -62,6 +62,9 @@ src/braket/experimental/algorithms/shors/shors.py
|
|
|
62
62
|
src/braket/experimental/algorithms/simons/__init__.py
|
|
63
63
|
src/braket/experimental/algorithms/simons/simons.md
|
|
64
64
|
src/braket/experimental/algorithms/simons/simons.py
|
|
65
|
+
src/braket/experimental/algorithms/variational_quantum_eigensolver/__init__.py
|
|
66
|
+
src/braket/experimental/algorithms/variational_quantum_eigensolver/variational_quantum_eigensolver.md
|
|
67
|
+
src/braket/experimental/algorithms/variational_quantum_eigensolver/variational_quantum_eigensolver.py
|
|
65
68
|
src/braket/experimental/auxiliary_functions/__init__.py
|
|
66
69
|
src/braket/experimental/auxiliary_functions/random_circuit/__init__.py
|
|
67
70
|
src/braket/experimental/auxiliary_functions/random_circuit/random_circuit.md
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
|
5
|
+
# the License is located at
|
|
6
|
+
#
|
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
|
8
|
+
#
|
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
|
12
|
+
# language governing permissions and limitations under the License.
|
|
13
|
+
|
|
14
|
+
from braket.experimental.algorithms.variational_quantum_eigensolver.variational_quantum_eigensolver import ( # noqa: F401
|
|
15
|
+
evaluate_energy,
|
|
16
|
+
exact_ground_state_energy,
|
|
17
|
+
get_parameter_names,
|
|
18
|
+
get_vqe_results,
|
|
19
|
+
group_commuting_terms,
|
|
20
|
+
hamiltonian_from_qubit_operator,
|
|
21
|
+
hamiltonian_matrix,
|
|
22
|
+
hardware_efficient_ansatz,
|
|
23
|
+
pauli_exponential_ansatz,
|
|
24
|
+
pauli_rotation,
|
|
25
|
+
run_vqe,
|
|
26
|
+
uccsd_generators,
|
|
27
|
+
vqe_circuits,
|
|
28
|
+
)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
The Variational Quantum Eigensolver (VQE) is a hybrid quantum-classical algorithm that approximates the ground state energy of a Hamiltonian. A parameterized quantum circuit (the ansatz) prepares a trial state, the quantum device estimates its energy by measuring the Pauli terms of the Hamiltonian, and a classical optimizer updates the circuit parameters to minimize that energy. By the variational principle the result is an upper bound on the true ground state energy. VQE is one of the leading candidates for quantum chemistry and materials simulation on near-term devices. This example computes the ground state energy of the hydrogen molecule and its dissociation curve.
|
|
2
|
+
|
|
3
|
+
<!--
|
|
4
|
+
[metadata-name]: Variational Quantum Eigensolver
|
|
5
|
+
[metadata-tags]: Textbook
|
|
6
|
+
[metadata-url]: https://github.com/amazon-braket/amazon-braket-algorithm-library/tree/main/src/braket/experimental/algorithms/variational_quantum_eigensolver
|
|
7
|
+
-->
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License"). You
|
|
4
|
+
# may not use this file except in compliance with the License. A copy of
|
|
5
|
+
# the License is located at
|
|
6
|
+
#
|
|
7
|
+
# http://aws.amazon.com/apache2.0/
|
|
8
|
+
#
|
|
9
|
+
# or in the "license" file accompanying this file. This file is
|
|
10
|
+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
11
|
+
# ANY KIND, either express or implied. See the License for the specific
|
|
12
|
+
# language governing permissions and limitations under the License.
|
|
13
|
+
|
|
14
|
+
import re
|
|
15
|
+
from functools import reduce
|
|
16
|
+
from itertools import combinations, pairwise
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
from openfermion import FermionOperator, hermitian_conjugated, jordan_wigner
|
|
21
|
+
from scipy.optimize import minimize
|
|
22
|
+
|
|
23
|
+
from braket.circuits import Circuit, FreeParameter, FreeParameterExpression, Instruction, Observable
|
|
24
|
+
from braket.devices import Device
|
|
25
|
+
|
|
26
|
+
# A Hamiltonian is a mapping from Pauli strings to real coefficients, e.g.
|
|
27
|
+
# {"II": -1.05, "ZI": 0.39, "XX": 0.18}. The i-th character acts on qubit i.
|
|
28
|
+
Hamiltonian = dict[str, float]
|
|
29
|
+
|
|
30
|
+
CHEMICAL_ACCURACY = 1.6e-3 # Hartree
|
|
31
|
+
|
|
32
|
+
_PAULI_OBSERVABLES = {"X": Observable.X, "Y": Observable.Y, "Z": Observable.Z}
|
|
33
|
+
_PAULI_MATRICES = {
|
|
34
|
+
"I": np.eye(2),
|
|
35
|
+
"X": np.array([[0, 1], [1, 0]]),
|
|
36
|
+
"Y": np.array([[0, -1j], [1j, 0]]),
|
|
37
|
+
"Z": np.array([[1, 0], [0, -1]]),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def hardware_efficient_ansatz(
|
|
42
|
+
n_qubits: int, n_layers: int, initial_state: list[int] | None = None
|
|
43
|
+
) -> Circuit:
|
|
44
|
+
"""Hardware-efficient ansatz made of RY rotations and a linear chain of CNOTs.
|
|
45
|
+
|
|
46
|
+
Each layer applies an RY rotation to every qubit followed by CNOTs between
|
|
47
|
+
neighbouring qubits. A final layer of RY rotations is appended, so the circuit has
|
|
48
|
+
n_qubits * (n_layers + 1) free parameters named theta_0, theta_1, ...
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
n_qubits (int): Number of qubits.
|
|
52
|
+
n_layers (int): Number of entangling layers.
|
|
53
|
+
initial_state (list[int] | None): Computational basis state to start from, e.g.
|
|
54
|
+
the Hartree-Fock state [1, 1, 0, 0]. Defaults to the all-zero state.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
Circuit: The parametric ansatz circuit.
|
|
58
|
+
"""
|
|
59
|
+
if n_qubits < 1:
|
|
60
|
+
raise ValueError("n_qubits must be at least 1.")
|
|
61
|
+
if n_layers < 0:
|
|
62
|
+
raise ValueError("n_layers must be non-negative.")
|
|
63
|
+
if initial_state is not None and len(initial_state) != n_qubits:
|
|
64
|
+
raise ValueError("initial_state must have one entry per qubit.")
|
|
65
|
+
|
|
66
|
+
circ = Circuit()
|
|
67
|
+
if initial_state is not None:
|
|
68
|
+
for qubit, bit in enumerate(initial_state):
|
|
69
|
+
if bit:
|
|
70
|
+
circ.x(qubit)
|
|
71
|
+
|
|
72
|
+
parameter_index = 0
|
|
73
|
+
for layer in range(n_layers + 1):
|
|
74
|
+
for qubit in range(n_qubits):
|
|
75
|
+
circ.ry(qubit, FreeParameter(f"theta_{parameter_index}"))
|
|
76
|
+
parameter_index += 1
|
|
77
|
+
if layer < n_layers:
|
|
78
|
+
for qubit in range(n_qubits - 1):
|
|
79
|
+
circ.cnot(qubit, qubit + 1)
|
|
80
|
+
return circ
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def pauli_rotation(pauli_string: str, angle: float | FreeParameterExpression) -> Circuit:
|
|
84
|
+
"""Circuit implementing exp(-i * angle / 2 * P) for a Pauli string P.
|
|
85
|
+
|
|
86
|
+
Each qubit in the support of P is rotated into the Z basis, the parity of the
|
|
87
|
+
support is accumulated with a CNOT ladder, an RZ rotation is applied and the
|
|
88
|
+
ladder and basis changes are undone.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
pauli_string (str): Pauli string, e.g. "XZY". The i-th character acts on qubit i.
|
|
92
|
+
angle (float | FreeParameterExpression): Rotation angle.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
Circuit: The Pauli rotation circuit.
|
|
96
|
+
"""
|
|
97
|
+
_validate_hamiltonian({pauli_string: 1.0})
|
|
98
|
+
support = [q for q, p in enumerate(pauli_string) if p != "I"]
|
|
99
|
+
circ = Circuit()
|
|
100
|
+
if not support:
|
|
101
|
+
return circ.i(range(len(pauli_string)))
|
|
102
|
+
|
|
103
|
+
def change_basis(sign: int) -> None:
|
|
104
|
+
for qubit in support:
|
|
105
|
+
if pauli_string[qubit] == "X":
|
|
106
|
+
circ.h(qubit)
|
|
107
|
+
elif pauli_string[qubit] == "Y":
|
|
108
|
+
circ.rx(qubit, sign * np.pi / 2)
|
|
109
|
+
|
|
110
|
+
change_basis(1)
|
|
111
|
+
for control, target in pairwise(support):
|
|
112
|
+
circ.cnot(control, target)
|
|
113
|
+
circ.rz(support[-1], angle)
|
|
114
|
+
for control, target in reversed(list(pairwise(support))):
|
|
115
|
+
circ.cnot(control, target)
|
|
116
|
+
change_basis(-1)
|
|
117
|
+
return circ
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def pauli_exponential_ansatz(
|
|
121
|
+
generators: list[Hamiltonian], initial_state: list[int] | None = None
|
|
122
|
+
) -> Circuit:
|
|
123
|
+
"""Ansatz prod_k exp(-i * theta_k * G_k) for Hermitian generators G_k.
|
|
124
|
+
|
|
125
|
+
Each generator is a sum of Pauli strings G_k = sum_j c_j P_j and is implemented as
|
|
126
|
+
the product of Pauli rotations exp(-i * theta_k * c_j * P_j). This is exact when the
|
|
127
|
+
Pauli strings of a generator commute, as is the case for fermionic excitations.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
generators (list[Hamiltonian]): Hermitian generators, one per free parameter.
|
|
131
|
+
initial_state (list[int] | None): Computational basis state to start from, e.g.
|
|
132
|
+
the Hartree-Fock state [1, 1, 0, 0]. Defaults to the all-zero state.
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
Circuit: The parametric ansatz circuit with parameters theta_0, theta_1, ...
|
|
136
|
+
"""
|
|
137
|
+
if not generators:
|
|
138
|
+
raise ValueError("At least one generator is required.")
|
|
139
|
+
for generator in generators:
|
|
140
|
+
_validate_hamiltonian(generator)
|
|
141
|
+
n_qubits = len(next(iter(generators[0])))
|
|
142
|
+
if any(len(p) != n_qubits for generator in generators for p in generator):
|
|
143
|
+
raise ValueError("All generators must act on the same number of qubits.")
|
|
144
|
+
if initial_state is not None and len(initial_state) != n_qubits:
|
|
145
|
+
raise ValueError("initial_state must have one entry per qubit.")
|
|
146
|
+
|
|
147
|
+
circ = Circuit()
|
|
148
|
+
if initial_state is not None:
|
|
149
|
+
for qubit, bit in enumerate(initial_state):
|
|
150
|
+
if bit:
|
|
151
|
+
circ.x(qubit)
|
|
152
|
+
for index, generator in enumerate(generators):
|
|
153
|
+
theta = FreeParameter(f"theta_{index}")
|
|
154
|
+
for pauli_string, coefficient in generator.items():
|
|
155
|
+
if set(pauli_string) != {"I"}:
|
|
156
|
+
circ.add_circuit(pauli_rotation(pauli_string, 2 * coefficient * theta))
|
|
157
|
+
return circ
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def uccsd_generators(n_qubits: int, n_electrons: int) -> list[Hamiltonian]:
|
|
161
|
+
"""Jordan-Wigner encoded generators of the UCCSD ansatz.
|
|
162
|
+
|
|
163
|
+
Spin orbitals are ordered with alternating spin (even indices spin up, odd indices
|
|
164
|
+
spin down), as in OpenFermion, and the Hartree-Fock state occupies the first
|
|
165
|
+
n_electrons spin orbitals. Only spin-conserving single and double excitations are
|
|
166
|
+
included. Each excitation T gives the Hermitian generator G = i(T - T^dagger), so that
|
|
167
|
+
exp(-i * theta * G) = exp(theta * (T - T^dagger)).
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
n_qubits (int): Number of spin orbitals (qubits).
|
|
171
|
+
n_electrons (int): Number of electrons.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
list[Hamiltonian]: One generator per single and double excitation.
|
|
175
|
+
"""
|
|
176
|
+
if not 0 < n_electrons < n_qubits:
|
|
177
|
+
raise ValueError("n_electrons must be between 1 and n_qubits - 1.")
|
|
178
|
+
occupied = range(n_electrons)
|
|
179
|
+
virtual = range(n_electrons, n_qubits)
|
|
180
|
+
|
|
181
|
+
excitations = [((a, 1), (i, 0)) for i in occupied for a in virtual if i % 2 == a % 2] + [
|
|
182
|
+
((a, 1), (b, 1), (j, 0), (i, 0))
|
|
183
|
+
for i, j in combinations(occupied, 2)
|
|
184
|
+
for a, b in combinations(virtual, 2)
|
|
185
|
+
if sorted([i % 2, j % 2]) == sorted([a % 2, b % 2])
|
|
186
|
+
]
|
|
187
|
+
|
|
188
|
+
generators = []
|
|
189
|
+
for excitation in excitations:
|
|
190
|
+
operator = FermionOperator(excitation)
|
|
191
|
+
generator = jordan_wigner(1j * (operator - hermitian_conjugated(operator)))
|
|
192
|
+
generators.append(hamiltonian_from_qubit_operator(generator, n_qubits))
|
|
193
|
+
return generators
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def hamiltonian_from_qubit_operator(qubit_operator: Any, n_qubits: int) -> Hamiltonian:
|
|
197
|
+
"""Convert an OpenFermion QubitOperator into a Hamiltonian dictionary.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
qubit_operator (Any): An object with a `terms` attribute mapping tuples of
|
|
201
|
+
(qubit, pauli) pairs to coefficients, such as an OpenFermion QubitOperator.
|
|
202
|
+
n_qubits (int): Number of qubits the Hamiltonian acts on.
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
Hamiltonian: Mapping from Pauli strings to real coefficients.
|
|
206
|
+
"""
|
|
207
|
+
hamiltonian = {}
|
|
208
|
+
for term, coefficient in qubit_operator.terms.items():
|
|
209
|
+
if not np.isclose(np.imag(coefficient), 0):
|
|
210
|
+
raise ValueError("Hamiltonian coefficients must be real.")
|
|
211
|
+
pauli = ["I"] * n_qubits
|
|
212
|
+
for qubit, operator in term:
|
|
213
|
+
if qubit >= n_qubits:
|
|
214
|
+
raise ValueError(f"Term {term} acts outside of {n_qubits} qubits.")
|
|
215
|
+
pauli[qubit] = operator
|
|
216
|
+
pauli_string = "".join(pauli)
|
|
217
|
+
hamiltonian[pauli_string] = hamiltonian.get(pauli_string, 0.0) + float(np.real(coefficient))
|
|
218
|
+
return hamiltonian
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def hamiltonian_matrix(hamiltonian: Hamiltonian) -> np.ndarray:
|
|
222
|
+
"""Dense matrix representation of a Hamiltonian.
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
226
|
+
|
|
227
|
+
Returns:
|
|
228
|
+
np.ndarray: The 2**n x 2**n Hamiltonian matrix, with qubit 0 as the most
|
|
229
|
+
significant bit.
|
|
230
|
+
"""
|
|
231
|
+
_validate_hamiltonian(hamiltonian)
|
|
232
|
+
return sum(
|
|
233
|
+
coefficient * reduce(np.kron, [_PAULI_MATRICES[p] for p in pauli_string])
|
|
234
|
+
for pauli_string, coefficient in hamiltonian.items()
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def exact_ground_state_energy(hamiltonian: Hamiltonian) -> float:
|
|
239
|
+
"""Lowest eigenvalue of a Hamiltonian obtained by exact diagonalization.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
float: The ground state energy.
|
|
246
|
+
"""
|
|
247
|
+
return float(np.linalg.eigvalsh(hamiltonian_matrix(hamiltonian))[0])
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def group_commuting_terms(hamiltonian: Hamiltonian) -> list[list[str]]:
|
|
251
|
+
"""Greedily partition the non-identity terms into qubit-wise commuting groups.
|
|
252
|
+
|
|
253
|
+
Terms in the same group can be measured simultaneously from a single circuit.
|
|
254
|
+
|
|
255
|
+
Args:
|
|
256
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
list[list[str]]: Groups of qubit-wise commuting Pauli strings.
|
|
260
|
+
"""
|
|
261
|
+
_validate_hamiltonian(hamiltonian)
|
|
262
|
+
groups = []
|
|
263
|
+
terms = sorted((p for p in hamiltonian if set(p) != {"I"}), key=lambda p: -abs(hamiltonian[p]))
|
|
264
|
+
for pauli_string in terms:
|
|
265
|
+
for group in groups:
|
|
266
|
+
if all(_qubit_wise_commute(pauli_string, member) for member in group):
|
|
267
|
+
group.append(pauli_string)
|
|
268
|
+
break
|
|
269
|
+
else:
|
|
270
|
+
groups.append([pauli_string])
|
|
271
|
+
return groups
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def vqe_circuits(ansatz: Circuit, hamiltonian: Hamiltonian, shots: int = 0) -> list[Circuit]:
|
|
275
|
+
"""Circuits that measure the energy of the ansatz state.
|
|
276
|
+
|
|
277
|
+
For shots=0 a single circuit with an expectation result type per term is returned.
|
|
278
|
+
For shots>0 one circuit per qubit-wise commuting group is returned, with basis
|
|
279
|
+
rotations appended so that all terms of the group are read out in the Z basis.
|
|
280
|
+
|
|
281
|
+
Args:
|
|
282
|
+
ansatz (Circuit): Parametric circuit preparing the trial state.
|
|
283
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
284
|
+
shots (int): Number of shots. Defaults to 0.
|
|
285
|
+
|
|
286
|
+
Returns:
|
|
287
|
+
list[Circuit]: The measurement circuits.
|
|
288
|
+
"""
|
|
289
|
+
_validate_hamiltonian(hamiltonian)
|
|
290
|
+
n_qubits = len(next(iter(hamiltonian)))
|
|
291
|
+
if ansatz.qubits and max(int(q) for q in ansatz.qubits) >= n_qubits:
|
|
292
|
+
raise ValueError("The ansatz acts on more qubits than the Hamiltonian.")
|
|
293
|
+
if ansatz.result_types:
|
|
294
|
+
raise ValueError("The ansatz must not contain result types.")
|
|
295
|
+
|
|
296
|
+
base = ansatz.copy()
|
|
297
|
+
unused_qubits = sorted(set(range(n_qubits)) - {int(q) for q in ansatz.qubits})
|
|
298
|
+
if unused_qubits:
|
|
299
|
+
base.i(unused_qubits)
|
|
300
|
+
|
|
301
|
+
if shots == 0:
|
|
302
|
+
circ = base.copy()
|
|
303
|
+
for pauli_string in hamiltonian:
|
|
304
|
+
if set(pauli_string) != {"I"}:
|
|
305
|
+
observable, targets = _pauli_observable(pauli_string)
|
|
306
|
+
circ.expectation(observable=observable, target=targets)
|
|
307
|
+
return [circ]
|
|
308
|
+
|
|
309
|
+
circuits = []
|
|
310
|
+
for group in group_commuting_terms(hamiltonian):
|
|
311
|
+
circ = base.copy()
|
|
312
|
+
for qubit in range(n_qubits):
|
|
313
|
+
basis = next((p[qubit] for p in group if p[qubit] != "I"), "I")
|
|
314
|
+
if basis != "I":
|
|
315
|
+
for gate in _PAULI_OBSERVABLES[basis]().basis_rotation_gates:
|
|
316
|
+
circ.add_instruction(Instruction(gate, qubit))
|
|
317
|
+
circuits.append(circ)
|
|
318
|
+
return circuits
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def evaluate_energy(
|
|
322
|
+
device: Device,
|
|
323
|
+
ansatz: Circuit,
|
|
324
|
+
hamiltonian: Hamiltonian,
|
|
325
|
+
parameter_values: np.ndarray,
|
|
326
|
+
shots: int = 0,
|
|
327
|
+
) -> float:
|
|
328
|
+
"""Estimate the energy of the ansatz state for the given parameter values.
|
|
329
|
+
|
|
330
|
+
Args:
|
|
331
|
+
device (Device): Braket device to run on.
|
|
332
|
+
ansatz (Circuit): Parametric circuit preparing the trial state.
|
|
333
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
334
|
+
parameter_values (np.ndarray): Values of the ansatz parameters, ordered as
|
|
335
|
+
returned by `get_parameter_names`.
|
|
336
|
+
shots (int): Number of shots per measurement circuit. Defaults to 0.
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
float: The estimated energy.
|
|
340
|
+
"""
|
|
341
|
+
names = get_parameter_names(ansatz)
|
|
342
|
+
if len(parameter_values) != len(names):
|
|
343
|
+
raise ValueError(f"Expected {len(names)} parameter values, got {len(parameter_values)}.")
|
|
344
|
+
bindings = dict(zip(names, (float(v) for v in parameter_values)))
|
|
345
|
+
circuits = [c.make_bound_circuit(bindings) for c in vqe_circuits(ansatz, hamiltonian, shots)]
|
|
346
|
+
|
|
347
|
+
energy = sum(c for p, c in hamiltonian.items() if set(p) == {"I"})
|
|
348
|
+
terms = [p for p in hamiltonian if set(p) != {"I"}]
|
|
349
|
+
if not terms:
|
|
350
|
+
return float(energy)
|
|
351
|
+
if shots == 0:
|
|
352
|
+
result = device.run(circuits[0], shots=0).result()
|
|
353
|
+
return float(energy + sum(hamiltonian[p] * v for p, v in zip(terms, result.values)))
|
|
354
|
+
|
|
355
|
+
tasks = [device.run(circ, shots=shots) for circ in circuits]
|
|
356
|
+
for group, task in zip(group_commuting_terms(hamiltonian), tasks):
|
|
357
|
+
result = task.result()
|
|
358
|
+
columns = {int(q): i for i, q in enumerate(result.measured_qubits)}
|
|
359
|
+
signs = 1 - 2 * np.asarray(result.measurements)
|
|
360
|
+
for pauli_string in group:
|
|
361
|
+
support = [columns[q] for q, p in enumerate(pauli_string) if p != "I"]
|
|
362
|
+
energy += hamiltonian[pauli_string] * np.mean(np.prod(signs[:, support], axis=1))
|
|
363
|
+
return float(energy)
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
def run_vqe(
|
|
367
|
+
device: Device,
|
|
368
|
+
hamiltonian: Hamiltonian,
|
|
369
|
+
ansatz: Circuit,
|
|
370
|
+
initial_parameters: np.ndarray,
|
|
371
|
+
shots: int = 0,
|
|
372
|
+
optimizer: str = "COBYLA",
|
|
373
|
+
optimizer_options: dict | None = None,
|
|
374
|
+
) -> dict[str, Any]:
|
|
375
|
+
"""Run the variational quantum eigensolver.
|
|
376
|
+
|
|
377
|
+
The ansatz parameters are optimized classically to minimize the energy
|
|
378
|
+
<psi(theta)|H|psi(theta)> evaluated on the quantum device.
|
|
379
|
+
|
|
380
|
+
Args:
|
|
381
|
+
device (Device): Braket device to run on.
|
|
382
|
+
hamiltonian (Hamiltonian): Mapping from Pauli strings to coefficients.
|
|
383
|
+
ansatz (Circuit): Parametric circuit preparing the trial state.
|
|
384
|
+
initial_parameters (np.ndarray): Starting parameter values, ordered as returned by
|
|
385
|
+
`get_parameter_names`.
|
|
386
|
+
shots (int): Number of shots per measurement circuit. Defaults to 0 (exact).
|
|
387
|
+
optimizer (str): Any method accepted by `scipy.optimize.minimize`.
|
|
388
|
+
Defaults to "COBYLA".
|
|
389
|
+
optimizer_options (dict | None): Options passed to the optimizer.
|
|
390
|
+
|
|
391
|
+
Returns:
|
|
392
|
+
dict[str, Any]: Dictionary with the optimal energy, optimal parameters, the
|
|
393
|
+
energy at every evaluation and the raw `scipy.optimize.OptimizeResult`.
|
|
394
|
+
"""
|
|
395
|
+
energy_history = []
|
|
396
|
+
|
|
397
|
+
def cost(parameter_values: np.ndarray) -> float:
|
|
398
|
+
energy = evaluate_energy(device, ansatz, hamiltonian, parameter_values, shots)
|
|
399
|
+
energy_history.append(energy)
|
|
400
|
+
return energy
|
|
401
|
+
|
|
402
|
+
optimizer_result = minimize(
|
|
403
|
+
cost,
|
|
404
|
+
np.asarray(initial_parameters, dtype=float),
|
|
405
|
+
method=optimizer,
|
|
406
|
+
options=optimizer_options,
|
|
407
|
+
)
|
|
408
|
+
return {
|
|
409
|
+
"optimal_energy": float(optimizer_result.fun),
|
|
410
|
+
"optimal_parameters": optimizer_result.x,
|
|
411
|
+
"energy_history": energy_history,
|
|
412
|
+
"optimizer_result": optimizer_result,
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def get_vqe_results(
|
|
417
|
+
vqe_result: dict[str, Any], exact_energy: float | None = None, verbose: bool = True
|
|
418
|
+
) -> dict[str, Any]:
|
|
419
|
+
"""Summarize the output of `run_vqe`.
|
|
420
|
+
|
|
421
|
+
Args:
|
|
422
|
+
vqe_result (dict[str, Any]): Output of `run_vqe`.
|
|
423
|
+
exact_energy (float | None): Reference energy to compare against, if known.
|
|
424
|
+
verbose (bool): Whether to print the summary. Defaults to True.
|
|
425
|
+
|
|
426
|
+
Returns:
|
|
427
|
+
dict[str, Any]: The VQE energy, number of energy evaluations and, if a reference
|
|
428
|
+
energy is given, the absolute error and whether chemical accuracy is reached.
|
|
429
|
+
"""
|
|
430
|
+
summary = {
|
|
431
|
+
"vqe_energy": vqe_result["optimal_energy"],
|
|
432
|
+
"n_evaluations": len(vqe_result["energy_history"]),
|
|
433
|
+
}
|
|
434
|
+
if exact_energy is not None:
|
|
435
|
+
error = abs(vqe_result["optimal_energy"] - exact_energy)
|
|
436
|
+
summary.update(
|
|
437
|
+
exact_energy=exact_energy,
|
|
438
|
+
absolute_error=error,
|
|
439
|
+
chemical_accuracy=bool(error < CHEMICAL_ACCURACY),
|
|
440
|
+
)
|
|
441
|
+
if verbose:
|
|
442
|
+
print(f"VQE energy: {summary['vqe_energy']:.6f}")
|
|
443
|
+
print(f"Energy evaluations: {summary['n_evaluations']}")
|
|
444
|
+
if exact_energy is not None:
|
|
445
|
+
print(f"Exact energy: {exact_energy:.6f}")
|
|
446
|
+
print(f"Absolute error: {summary['absolute_error']:.2e}")
|
|
447
|
+
print(f"Chemical accuracy: {summary['chemical_accuracy']}")
|
|
448
|
+
return summary
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def get_parameter_names(circ: Circuit) -> list[str]:
|
|
452
|
+
"""Names of the free parameters of a circuit in natural sort order (theta_2 < theta_10).
|
|
453
|
+
|
|
454
|
+
Args:
|
|
455
|
+
circ (Circuit): A parametric circuit.
|
|
456
|
+
|
|
457
|
+
Returns:
|
|
458
|
+
list[str]: The sorted parameter names.
|
|
459
|
+
"""
|
|
460
|
+
|
|
461
|
+
def natural_key(name: str) -> list:
|
|
462
|
+
return [int(t) if t.isdigit() else t for t in re.split(r"(\d+)", name)]
|
|
463
|
+
|
|
464
|
+
return sorted((p.name for p in circ.parameters), key=natural_key)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def _validate_hamiltonian(hamiltonian: Hamiltonian) -> None:
|
|
468
|
+
if not hamiltonian:
|
|
469
|
+
raise ValueError("The Hamiltonian must contain at least one term.")
|
|
470
|
+
lengths = {len(p) for p in hamiltonian}
|
|
471
|
+
if len(lengths) != 1:
|
|
472
|
+
raise ValueError("All Pauli strings must have the same length.")
|
|
473
|
+
if not all(set(p) <= set("IXYZ") for p in hamiltonian):
|
|
474
|
+
raise ValueError("Pauli strings may only contain the characters I, X, Y and Z.")
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def _qubit_wise_commute(a: str, b: str) -> bool:
|
|
478
|
+
return all(p == "I" or q == "I" or p == q for p, q in zip(a, b))
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def _pauli_observable(pauli_string: str) -> tuple[Observable, list[int]]:
|
|
482
|
+
targets = [q for q, p in enumerate(pauli_string) if p != "I"]
|
|
483
|
+
observable = reduce(
|
|
484
|
+
lambda a, b: a @ b, [_PAULI_OBSERVABLES[pauli_string[q]]() for q in targets]
|
|
485
|
+
)
|
|
486
|
+
return observable, targets
|
|
File without changes
|
|
File without changes
|
{amazon_braket_algorithm_library-1.7.10 → amazon_braket_algorithm_library-1.8.0}/pyproject.toml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|