-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathqaoa_circuit_labs.py
More file actions
77 lines (69 loc) · 2.92 KB
/
Copy pathqaoa_circuit_labs.py
File metadata and controls
77 lines (69 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
###############################################################################
# // SPDX-License-Identifier: Apache-2.0
# // Copyright : JP Morgan Chase & Co
###############################################################################
# QAOA circuit for some Z objective
from collections.abc import Sequence
from qiskit import QuantumCircuit
from qokit.labs import get_energy_term_indices
from .qaoa_circuit import get_qaoa_circuit_from_terms, get_parameterized_qaoa_circuit_from_terms
def get_qaoa_circuit(N: int, gammas: Sequence, betas: Sequence, save_statevector: bool = True) -> QuantumCircuit:
"""Generates a circuit for Hamiltonian of the form \sum_{term \in terms} \prod_{j \in term} Z_j
Parameters
----------
N : int
Number of qubits
beta : list-like
QAOA parameter beta
gamma : list-like
QAOA parameter gamma
save_statevector : bool, default True
Add save state instruction to the end of the circuit
Returns
-------
qc : qiskit.QuantumCircuit
Quantum circuit implementing QAOA
"""
terms, _ = get_energy_term_indices(N)
return get_qaoa_circuit_from_terms(N=N, terms=terms, gammas=gammas, betas=betas, save_statevector=save_statevector)
def get_parameterized_qaoa_circuit(N: int, p: int, save_statevector: bool = True, return_parameter_vectors: bool = False) -> QuantumCircuit:
"""Generates a parameterized circuit for Hamiltonian of the form \sum_{term \in terms} \prod_{j \in term} Z_j
This version is recommended for long circuits
Example usage:
qc_param = get_parameterized_qaoa_circuit(N, terms, p)
def f(theta):
...
qc = qc_param.bind_parameters(np.hstack([beta, gamma]))
sv = backend.run(qc).result().get_statevector()
...
minimize(f, ...)
Parameters
----------
N : int
Number of qubits
terms : list of tuples
Each tuple corresponds to a term \prod_{j \in term} Z_j and contains indices
Example: for H = Z_0*Z_1 + Z_2*Z_3 + Z_0*Z_2*Z_4, terms = [(0,1), (2,3), (0,2,4)]
All indices must be less than N
p : int
Number of QAOA layers (number of parameters will be 2*p)
save_statevector : bool, default True
Add save state instruction to the end of the circuit
return_parameter_vectors : bool, default False
Return ParameterVector for betas and gammas
Returns
-------
qc : qiskit.QuantumCircuit
Parameterized quantum circuit implementing QAOA
Parameters are two ParameterVector sorted alphabetically
(beta first, then gamma). To bind:
qc.bind_parameters(np.hstack([angles['beta'], angles['gamma']]))
"""
terms, _ = get_energy_term_indices(N)
return get_parameterized_qaoa_circuit_from_terms(
N=N,
terms=terms,
p=p,
save_statevector=save_statevector,
return_parameter_vectors=return_parameter_vectors,
)