Skip to content

Commit 307a8e9

Browse files
committed
black
1 parent 742f880 commit 307a8e9

10 files changed

Lines changed: 97 additions & 65 deletions

File tree

qokit/fur/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ def choose_simulator(name="auto", **kwargs):
8282

8383
return get_available_simulators("x")[0]
8484

85+
8586
def choose_simulator_xz(name="auto", **kwargs):
8687
if name != "auto":
8788
return SIMULATORS["xz"][name]
8889

8990
return get_available_simulators("xz")[0]
9091

92+
9193
def choose_simulator_xyring(name="auto", **kwargs):
9294
if name != "auto":
9395
return SIMULATORS["xyring"][name]

qokit/fur/c/csim/wrapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def apply_qaoa_furx(
6161
n_layers,
6262
)
6363

64+
6465
def apply_qaoa_furxz(
6566
sv_real: np.ndarray,
6667
sv_imag: np.ndarray,
@@ -80,13 +81,14 @@ def apply_qaoa_furxz(
8081
sv_imag,
8182
np.asarray(gammas, dtype="float"),
8283
np.asarray(betas, dtype="float"),
83-
np.asarray(init_rots, dtype="float"),
84+
np.asarray(init_rots, dtype="float"),
8485
hc_diag,
8586
n_qubits,
8687
n_states,
8788
n_layers,
8889
)
8990

91+
9092
def furxy(
9193
sv_real: np.ndarray,
9294
sv_imag: np.ndarray,

qokit/fur/c/gates.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def furx(sv: ComplexArray | np.ndarray, theta: float, q: int) -> ComplexArray:
2424
csim.furx(sv.real, sv.imag, 0.5 * theta, q)
2525
return sv
2626

27+
2728
def furxz(sv: ComplexArray | np.ndarray, theta: float, init_rots: np.ndarray, q: int) -> ComplexArray:
2829
"""
2930
apply to a statevector a single-qubit Pauli-XZ rotation defined by
@@ -40,6 +41,7 @@ def furxz(sv: ComplexArray | np.ndarray, theta: float, init_rots: np.ndarray, q:
4041
csim.furxz(sv.real, sv.imag, 0.5 * theta, init_rots, q)
4142
return sv
4243

44+
4345
def furxy(sv: ComplexArray | np.ndarray, theta: float, q1: int, q2: int) -> ComplexArray:
4446
"""
4547
apply to a statevector a two-qubit XX+YY rotation defined by

qokit/fur/c/qaoa_simulator.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@ def default_sv0(self):
3131
np.full(self.n_states, 1.0 / np.sqrt(self.n_states), dtype="float"),
3232
np.zeros(self.n_states, dtype="float"),
3333
)
34-
34+
3535
def warmstart_sv0(self, init_rots):
3636
state = 1.0
37-
37+
3838
for init_rot in init_rots:
39-
qubit_state = np.array([
40-
np.cos(init_rot/2),
41-
np.sin(init_rot/2)
42-
# np.exp(-1j * np.pi/2) * np.sin(init_rot/2)
43-
])
39+
qubit_state = np.array(
40+
[
41+
np.cos(init_rot / 2),
42+
np.sin(init_rot / 2),
43+
# np.exp(-1j * np.pi/2) * np.sin(init_rot/2)
44+
]
45+
)
4446
# state = np.kron(state, qubit_state) #original as GW
4547
state = np.kron(qubit_state, state)
46-
48+
4749
return ComplexArray(state.real.astype("float"), state.imag.astype("float"))
4850

4951
def _apply_qaoa(self, sv: ComplexArray, gammas: Sequence[float], betas: Sequence[float], **kwargs):
5052
raise NotImplementedError
51-
53+
5254
def simulate_ws_qaoa(
5355
self,
5456
gammas: ParamType,
@@ -67,7 +69,7 @@ def simulate_ws_qaoa(
6769
sv = ComplexArray(sv0.real.astype("float"), sv0.imag.astype("float")) if sv0 is not None else self.warmstart_sv0(init_rots)
6870
self._apply_qaoa(sv, list(gammas), list(betas), init_rots, **kwargs)
6971
return sv
70-
72+
7173
def simulate_qaoa(
7274
self,
7375
gammas: ParamType,
@@ -91,13 +93,13 @@ def get_expectation(self, result: ComplexArray, costs: np.ndarray | None = None,
9193
if optimization_type == "max":
9294
costs = -1 * np.asarray(costs)
9395
return np.dot(costs, self.get_probabilities(result, **kwargs))
94-
96+
9597
def get_std(self, result: ComplexArray, costs: np.ndarray | None = None, **kwargs) -> float:
9698
if costs is None:
9799
costs = self._hc_diag
98100
probs = self.get_probabilities(result)
99101
return np.sqrt(max(np.dot(costs**2, probs) - np.dot(costs, probs) ** 2, 0))
100-
102+
101103
def get_overlap(
102104
self, result: ComplexArray, costs: CostsType | None = None, indices: np.ndarray | Sequence[int] | None = None, optimization_type="min", **kwargs
103105
) -> float:
@@ -135,6 +137,7 @@ def _apply_qaoa(self, sv: ComplexArray, gammas: Sequence[float], betas: Sequence
135137
self.n_qubits,
136138
)
137139

140+
138141
class QAOAFURXZSimulatorC(QAOAFastSimulatorCBase):
139142
def _apply_qaoa(self, sv: ComplexArray, gammas: Sequence[float], betas: Sequence[float], init_rots: Sequence[float], **kwargs):
140143
csim.apply_qaoa_furxz(
@@ -146,7 +149,7 @@ def _apply_qaoa(self, sv: ComplexArray, gammas: Sequence[float], betas: Sequence
146149
self._hc_diag,
147150
self.n_qubits,
148151
)
149-
152+
150153

151154
class QAOAFURXYRingSimulatorC(QAOAFastSimulatorCBase):
152155
def _apply_qaoa(self, sv: ComplexArray, gammas: Sequence[float], betas: Sequence[float], **kwargs):

qokit/fur/python/fur.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def furx_all(x: np.ndarray, theta: float, n_qubits: int) -> np.ndarray:
3838
furx(x, theta, i)
3939
return x
4040

41+
4142
def furxz(x: np.ndarray, theta: float, init_rot: float, q: int) -> np.ndarray:
4243
"""
4344
Applies e^{-i theta (sin(init_rot) X + cos(init_rot)Z) } on qubit indexed by q
@@ -48,32 +49,35 @@ def furxz(x: np.ndarray, theta: float, init_rot: float, q: int) -> np.ndarray:
4849
mask1 = (1 << q) - 1
4950
mask2 = mask1 ^ ((n_states - 1) >> 1)
5051

51-
cos_beta = math.cos(theta)
52-
sin_beta = math.sin(theta)
53-
52+
cos_beta = math.cos(theta)
53+
sin_beta = math.sin(theta)
54+
5455
sin_rot = math.sin(init_rot)
5556
cos_rot = math.cos(init_rot)
56-
57-
57+
5858
for i in range(n_groups):
5959
ia = (i & mask1) | ((i & mask2) << 1)
6060
ib = ia | (1 << q)
6161
# when phi = 0
62-
x[ia], x[ib] = (cos_beta - 1j * cos_rot*sin_beta ) * x[ia] - 1j*sin_rot * sin_beta * x[ib], -1j*sin_rot * sin_beta * x[ia] + (cos_beta + 1j * cos_rot*sin_beta )* x[ib]
63-
62+
x[ia], x[ib] = (cos_beta - 1j * cos_rot * sin_beta) * x[ia] - 1j * sin_rot * sin_beta * x[ib], -1j * sin_rot * sin_beta * x[ia] + (
63+
cos_beta + 1j * cos_rot * sin_beta
64+
) * x[ib]
65+
6466
# when phi = -pi/2
6567
# x[ia], x[ib] = (cos_beta - 1j * cos_rot*sin_beta ) * x[ia] + sin_rot * sin_beta * x[ib], -sin_rot * sin_beta * x[ia] + (cos_beta + 1j * cos_rot*sin_beta )* x[ib]
66-
68+
6769
return x
68-
69-
70+
71+
7072
def furxz_all(x: np.ndarray, theta: float, init_rots: list, n_qubits: int) -> np.ndarray:
7173
"""
7274
Applies e^{-i theta X} on all qubits
7375
"""
7476
for i in range(n_qubits):
7577
furxz(x, theta, init_rots[i], i)
7678
return x
79+
80+
7781
########################################
7882
# two-qubit XX+YY rotation
7983
########################################

qokit/fur/python/qaoa_fur.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def apply_qaoa_furx(sv: np.ndarray, gammas: Sequence[float], betas: Sequence[flo
2222
sv *= np.exp(-0.5j * np.array(gamma * hc_diag))
2323
furx_all(sv, beta, n_qubits)
2424

25+
2526
def apply_qaoa_furxz(sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], init_rots: Sequence[float], hc_diag: np.ndarray, n_qubits: int) -> None:
2627
"""
2728
apply a QAOA with the X mixer defined by
@@ -35,9 +36,10 @@ def apply_qaoa_furxz(sv: np.ndarray, gammas: Sequence[float], betas: Sequence[fl
3536
"""
3637
for gamma, beta in zip(gammas, betas):
3738
sv *= np.exp(-0.5j * gamma * hc_diag)
38-
assert np.isclose(np.sum(np.abs(sv ** 2)), 1)
39+
assert np.isclose(np.sum(np.abs(sv**2)), 1)
3940
furxz_all(sv, beta, init_rots, n_qubits)
40-
41+
42+
4143
def apply_qaoa_furxy_ring(sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], hc_diag: np.ndarray, n_qubits: int, n_trotters: int = 1) -> None:
4244
"""
4345
apply a QAOA with the XY-ring mixer defined by

qokit/fur/python/qaoa_simulator.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,25 @@ def get_cost_diagonal(self) -> np.ndarray:
3636
@property
3737
def default_sv0(self):
3838
return np.full(self.n_states, 1.0 / np.sqrt(self.n_states), dtype="complex")
39-
39+
4040
def warmstart_sv0(self, init_rots):
4141
state = 1.0
4242
for init_rot in init_rots:
43-
qubit_state = np.array([
44-
np.cos(init_rot/2),
45-
# np.exp(-1j*np.pi/2)*np.sin(init_rot/2)
46-
np.sin(init_rot/2)
47-
])
43+
qubit_state = np.array(
44+
[
45+
np.cos(init_rot / 2),
46+
# np.exp(-1j*np.pi/2)*np.sin(init_rot/2)
47+
np.sin(init_rot / 2),
48+
]
49+
)
4850
# state = np.kron(state, qubit_state) #original as GW
4951
state = np.kron(qubit_state, state)
50-
52+
5153
return state
5254

5355
def _apply_qaoa(self, sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], **kwargs):
5456
raise NotImplementedError
55-
57+
5658
def simulate_ws_qaoa(
5759
self,
5860
gammas: ParamType,
@@ -69,11 +71,11 @@ def simulate_ws_qaoa(
6971
@return statevector or vector of probabilities
7072
"""
7173
sv = sv0.astype("complex") if sv0 is not None else self.warmstart_sv0(init_rots).astype("complex")
72-
assert np.isclose(np.sum(np.abs(sv ** 2)), 1)
74+
assert np.isclose(np.sum(np.abs(sv**2)), 1)
7375
self._apply_qaoa(sv, list(gammas), list(betas), init_rots, **kwargs)
74-
assert np.isclose(np.sum(np.abs(sv ** 2)), 1)
76+
assert np.isclose(np.sum(np.abs(sv**2)), 1)
7577
return sv
76-
78+
7779
def simulate_qaoa(
7880
self,
7981
gammas: ParamType,
@@ -106,14 +108,13 @@ def get_expectation(self, result: np.ndarray, costs: np.ndarray | None = None, o
106108
if optimization_type == "max":
107109
return -1 * np.dot(costs, np.abs(result) ** 2)
108110
return np.dot(costs, np.abs(result) ** 2)
109-
111+
110112
def get_std(self, result: np.ndarray, costs: np.ndarray | None = None, **kwargs) -> float:
111113
if costs is None:
112114
costs = self._hc_diag
113115
probs = np.abs(result) ** 2
114116
return np.sqrt(max(np.dot(costs**2, probs) - np.dot(costs, probs) ** 2, 0))
115117

116-
117118
def get_overlap(
118119
self, result: np.ndarray, costs: CostsType | None = None, indices: np.ndarray | Sequence[int] | None = None, optimization_type="min", **kwargs
119120
) -> float:
@@ -145,10 +146,12 @@ class QAOAFURXSimulator(QAOAFastSimulatorPythonBase):
145146
def _apply_qaoa(self, sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], **kwargs):
146147
apply_qaoa_furx(sv, gammas, betas, self._hc_diag, self.n_qubits)
147148

149+
148150
class QAOAFURXZSimulator(QAOAFastSimulatorPythonBase):
149151
def _apply_qaoa(self, sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], init_rots: Sequence[float], **kwargs):
150152
apply_qaoa_furxz(sv, gammas, betas, init_rots, self._hc_diag, self.n_qubits)
151153

154+
152155
class QAOAFURXYRingSimulator(QAOAFastSimulatorPythonBase):
153156
def _apply_qaoa(self, sv: np.ndarray, gammas: Sequence[float], betas: Sequence[float], **kwargs):
154157
n_trotters = kwargs.get("n_trotters", 1)

qokit/qaoa_circuit.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,16 @@ def get_qaoa_circuit_from_terms(
127127
qc.save_statevector()
128128
return qc
129129

130+
130131
def get_ws_qaoa_circuit_from_terms(
131-
N: int, terms: Sequence, gammas: Sequence, betas: Sequence, thetas: Sequence, save_statevector: bool = True, qr: QuantumRegister = None, cr: ClassicalRegister = None
132+
N: int,
133+
terms: Sequence,
134+
gammas: Sequence,
135+
betas: Sequence,
136+
thetas: Sequence,
137+
save_statevector: bool = True,
138+
qr: QuantumRegister = None,
139+
cr: ClassicalRegister = None,
132140
):
133141
"""Generates a Qiskit circuit from Hamiltonian terms
134142
@@ -189,6 +197,7 @@ def get_ws_qaoa_circuit_from_terms(
189197
qc.save_statevector()
190198
return qc
191199

200+
192201
def get_parameterized_qaoa_circuit_from_terms(
193202
N: int,
194203
terms: Sequence,

qokit/qaoa_circuit_maxcut.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def get_qaoa_circuit(G: nx.Graph, gammas: Sequence, betas: Sequence, save_statev
4040
N = G.number_of_nodes()
4141
return get_qaoa_circuit_from_terms(N=N, terms=terms[:-1], gammas=gammas, betas=betas, save_statevector=save_statevector, qr=qr, cr=cr)
4242

43-
def get_ws_qaoa_circuit(G: nx.Graph, gammas: Sequence, betas: Sequence, thetas: Sequence, save_statevector: bool = True, qr: QuantumRegister = None, cr: ClassicalRegister = None):
43+
44+
def get_ws_qaoa_circuit(
45+
G: nx.Graph, gammas: Sequence, betas: Sequence, thetas: Sequence, save_statevector: bool = True, qr: QuantumRegister = None, cr: ClassicalRegister = None
46+
):
4447
"""Generates a circuit for weighted MaxCut on graph G.
4548
Parameters
4649
----------

0 commit comments

Comments
 (0)