Skip to content

Commit 1c11cbd

Browse files
Minor refactor
1 parent b59b2c6 commit 1c11cbd

6 files changed

Lines changed: 108 additions & 133 deletions

scripts/clifford_vqe_entangled.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
# geometry = [("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.74))] # H2 Molecule
4343

44-
# geometry = [
45-
# ("H", (-1.0, 0.0, -1.0)), ("H", (-1.0, 0.0, 1.00)),
46-
# ("H", (1.0, 0.0, -1.0)), ("H", (1.0, 0.0, 1.00))
47-
# ] # H4 Dissociation (hard for Hartree-Fock)
44+
geometry = [
45+
("H", (-1.0, 0.0, -1.0)), ("H", (-1.0, 0.0, 1.00)),
46+
("H", (1.0, 0.0, -1.0)), ("H", (1.0, 0.0, 1.00))
47+
] # H4 Dissociation (hard for Hartree-Fock)
4848

4949
# Helium (and lighter):
5050

@@ -70,12 +70,12 @@
7070
# geometry = [('N', (0.0, 0.0, 0.0)), ('N', (0.0, 0.0, 1.10))] # N2 Molecule
7171

7272
# Ammonia:
73-
geometry = [
74-
("N", (0.0000, 0.0000, 0.0000)), # Nitrogen at center
75-
("H", (0.9400, 0.0000, -0.3200)), # Hydrogen 1
76-
("H", (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
77-
("H", (-0.4700, -0.8130, -0.3200)), # Hydrogen 3
78-
]
73+
# geometry = [
74+
# ('N', (0.0000, 0.0000, 0.0000)), # Nitrogen at center
75+
# ('H', (0.9400, 0.0000, -0.3200)), # Hydrogen 1
76+
# ('H', (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
77+
# ('H', (-0.4700, -0.8130, -0.3200)) # Hydrogen 3
78+
# ]
7979

8080
# Oxygen (and lighter):
8181

@@ -234,12 +234,13 @@
234234

235235
# Now, `geometry` contains all 6 carbons and 6 hydrogens!
236236

237-
238237
# Step 2: Create OpenFermion molecule
239238
def geometry_to_atom_str(geometry):
240239
"""Convert list of (symbol, (x,y,z)) to Pyscf atom string."""
241-
return "; ".join(f"{symbol} {x:.10f} {y:.10f} {z:.10f}" for symbol, (x, y, z) in geometry)
242-
240+
return "; ".join(
241+
f"{symbol} {x:.10f} {y:.10f} {z:.10f}"
242+
for symbol, (x, y, z) in geometry
243+
)
243244

244245
def initial_energy(theta_bits, z_hamiltonian):
245246
energy = 0.0
@@ -319,7 +320,9 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
319320
break
320321

321322
if len(combos_list) < k:
322-
combos = np.array(list(item for sublist in itertools.combinations(z_qubits, k) for item in sublist))
323+
combos = np.array(list(
324+
item for sublist in itertools.combinations(z_qubits, k) for item in sublist
325+
))
323326
combos_list.append(combos)
324327
else:
325328
combos = combos_list[k - 1]
@@ -362,7 +365,6 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
362365

363366
return best_theta, min_energy
364367

365-
366368
is_charge_update = True
367369
while is_charge_update:
368370
is_charge_update = False
@@ -385,7 +387,7 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
385387

386388
for pauli_string, jw_coeff in jw_term.terms.items():
387389
# Skip terms with X or Y
388-
if any(p in ("X", "Y") for _, p in pauli_string):
390+
if any(p in ('X', 'Y') for _, p in pauli_string):
389391
continue
390392

391393
q = []
@@ -417,7 +419,7 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
417419

418420
if n_electrons != r_electrons or multiplicity != r_multiplicity:
419421
print()
420-
print("Regresssed electron count doesn't match the assumptions!")
422+
print("Regresssed electron count or multiplicity doesn't match the assumptions!")
421423
print("Running again with the natural parameters replacing your assumptions:")
422424
print(f"charge = {r_charge}")
423425
print(f"multiplicity = {r_multiplicity}")
@@ -453,7 +455,6 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
453455

454456
hamiltonian = qml.Hamiltonian(coeffs, observables)
455457

456-
457458
# Step 5: Variational fit
458459
def fit_entanglement(hamiltonian, best_theta, n_qubits, min_energy):
459460
# Fast low-width simulation:
@@ -473,14 +474,14 @@ def circuit(theta, delta):
473474
# qml.H(wires=i)
474475
# qml.RZ(delta[i], wires=i)
475476
# qml.H(wires=i)
476-
for i in range(n_qubits - 1):
477-
qml.CZ(wires=[i, i + 1])
478-
qml.CZ(wires=[n_qubits - 1, 0])
477+
for i in range(n_qubits-1):
478+
qml.CZ(wires=[i, i+1])
479+
qml.CZ(wires=[n_qubits-1, 0])
479480
return qml.expval(hamiltonian)
480481

481482
best_delta = nppl.zeros(n_qubits, dtype=float, requires_grad=True)
482483
delta = best_delta.copy()
483-
opt = qml.AdamOptimizer(stepsize=(np.pi / 1800)) # one tenth a degree
484+
opt = qml.AdamOptimizer(stepsize=(np.pi / 1800)) #one tenth a degree
484485
num_steps = 100
485486
for step in range(num_steps):
486487
delta = opt.step(lambda delta: circuit(best_theta, delta), delta)
@@ -493,7 +494,6 @@ def circuit(theta, delta):
493494

494495
return best_theta, best_delta, min_energy
495496

496-
497497
# Run threaded bootstrap
498498
theta, delta, min_energy = fit_entanglement(hamiltonian, theta, n_qubits, min_energy)
499499

scripts/clifford_vqe_min.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
# Step 1: Define the molecule (Hydrogen, Helium, Lithium, Carbon, Nitrogen, Oxygen)
1616

17-
# basis = "sto-3g" # Minimal Basis Set
18-
basis = "6-31g" # Larger basis set
17+
basis = "sto-3g" # Minimal Basis Set
18+
# basis = '6-31g' # Larger basis set
1919
# basis = 'cc-pVDZ' # Even larger basis set!
2020
multiplicity = 1 # singlet, closed shell, all electrons are paired (neutral molecules with full valence)
2121
# multiplicity = 2 # doublet, one unpaired electron (ex.: OH- radical)
@@ -29,12 +29,10 @@
2929

3030
# geometry = [("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.74))] # H2 Molecule
3131

32-
geometry = [
33-
("H", (-1.0, 0.0, -1.0)),
34-
("H", (-1.0, 0.0, 1.00)),
35-
("H", (1.0, 0.0, -1.0)),
36-
("H", (1.0, 0.0, 1.00)),
37-
] # H4 Dissociation (hard for Hartree-Fock)
32+
# geometry = [
33+
# ("H", (-1.0, 0.0, -1.0)), ("H", (-1.0, 0.0, 1.00)),
34+
# ("H", (1.0, 0.0, -1.0)), ("H", (1.0, 0.0, 1.00))
35+
# ] # H4 Dissociation (hard for Hartree-Fock)
3836

3937
# Helium (and lighter):
4038

@@ -60,12 +58,12 @@
6058
# geometry = [('N', (0.0, 0.0, 0.0)), ('N', (0.0, 0.0, 1.10))] # N2 Molecule
6159

6260
# Ammonia:
63-
# geometry = [
64-
# ('N', (0.0000, 0.0000, 0.0000)), # Nitrogen at center
65-
# ('H', (0.9400, 0.0000, -0.3200)), # Hydrogen 1
66-
# ('H', (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
67-
# ('H', (-0.4700, -0.8130, -0.3200)) # Hydrogen 3
68-
# ]
61+
geometry = [
62+
('N', (0.0000, 0.0000, 0.0000)), # Nitrogen at center
63+
('H', (0.9400, 0.0000, -0.3200)), # Hydrogen 1
64+
('H', (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
65+
('H', (-0.4700, -0.8130, -0.3200)) # Hydrogen 3
66+
]
6967

7068
# Oxygen (and lighter):
7169

@@ -224,12 +222,13 @@
224222

225223
# Now, `geometry` contains all 6 carbons and 6 hydrogens!
226224

227-
228225
# Step 2: Create OpenFermion molecule
229226
def geometry_to_atom_str(geometry):
230227
"""Convert list of (symbol, (x,y,z)) to Pyscf atom string."""
231-
return "; ".join(f"{symbol} {x:.10f} {y:.10f} {z:.10f}" for symbol, (x, y, z) in geometry)
232-
228+
return "; ".join(
229+
f"{symbol} {x:.10f} {y:.10f} {z:.10f}"
230+
for symbol, (x, y, z) in geometry
231+
)
233232

234233
def initial_energy(theta_bits, z_hamiltonian):
235234
energy = 0.0
@@ -309,7 +308,9 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
309308
break
310309

311310
if len(combos_list) < k:
312-
combos = np.array(list(item for sublist in itertools.combinations(z_qubits, k) for item in sublist))
311+
combos = np.array(list(
312+
item for sublist in itertools.combinations(z_qubits, k) for item in sublist
313+
))
313314
combos_list.append(combos)
314315
else:
315316
combos = combos_list[k - 1]
@@ -352,7 +353,6 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
352353

353354
return best_theta, min_energy
354355

355-
356356
is_charge_update = True
357357
while is_charge_update:
358358
is_charge_update = False
@@ -375,7 +375,7 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
375375

376376
for pauli_string, jw_coeff in jw_term.terms.items():
377377
# Skip terms with X or Y
378-
if any(p in ("X", "Y") for _, p in pauli_string):
378+
if any(p in ('X', 'Y') for _, p in pauli_string):
379379
continue
380380

381381
q = []
@@ -407,7 +407,7 @@ def multiprocessing_bootstrap(z_hamiltonian, z_qubits, n_qubits, reheat_tries=0)
407407

408408
if n_electrons != r_electrons or multiplicity != r_multiplicity:
409409
print()
410-
print("Regresssed electron count doesn't match the assumptions!")
410+
print("Regresssed electron count or multiplicity doesn't match the assumptions!")
411411
print("Running again with the natural parameters replacing your assumptions:")
412412
print(f"charge = {r_charge}")
413413
print(f"multiplicity = {r_multiplicity}")

scripts/clifford_vqe_min_zx.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959

6060
# Ammonia:
6161
geometry = [
62-
("N", (0.0000, 0.0000, 0.0000)), # Nitrogen at center
63-
("H", (0.9400, 0.0000, -0.3200)), # Hydrogen 1
64-
("H", (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
65-
("H", (-0.4700, -0.8130, -0.3200)), # Hydrogen 3
62+
('N', (0.0000, 0.0000, 0.0000)), # Nitrogen at center
63+
('H', (0.9400, 0.0000, -0.3200)), # Hydrogen 1
64+
('H', (-0.4700, 0.8130, -0.3200)), # Hydrogen 2
65+
('H', (-0.4700, -0.8130, -0.3200)) # Hydrogen 3
6666
]
6767

6868
# Oxygen (and lighter):
@@ -222,12 +222,13 @@
222222

223223
# Now, `geometry` contains all 6 carbons and 6 hydrogens!
224224

225-
226225
# Step 2: Create OpenFermion molecule
227226
def geometry_to_atom_str(geometry):
228227
"""Convert list of (symbol, (x,y,z)) to Pyscf atom string."""
229-
return "; ".join(f"{symbol} {x:.10f} {y:.10f} {z:.10f}" for symbol, (x, y, z) in geometry)
230-
228+
return "; ".join(
229+
f"{symbol} {x:.10f} {y:.10f} {z:.10f}"
230+
for symbol, (x, y, z) in geometry
231+
)
231232

232233
def compute_energy(theta_bits, phi_bits, zx_hamiltonian):
233234
energy = 0.0
@@ -273,16 +274,7 @@ def bootstrap(theta, phi, zx_hamiltonian, k, indices_array, energy):
273274
args.append((theta, phi, zx_hamiltonian, [], indices_array[j : j + k], energy))
274275
for i in range(n):
275276
j = i * k
276-
args.append(
277-
(
278-
theta,
279-
phi,
280-
zx_hamiltonian,
281-
indices_array[j : j + k],
282-
indices_array[j : j + k],
283-
energy,
284-
)
285-
)
277+
args.append((theta, phi, zx_hamiltonian, indices_array[j : j + k], indices_array[j : j + k], energy))
286278
energies = pool.starmap(bootstrap_worker, args)
287279

288280
return energies
@@ -308,7 +300,9 @@ def multiprocessing_bootstrap(zx_hamiltonian, n_qubits, reheat_tries=0):
308300
break
309301

310302
if len(combos_list) < k:
311-
combos = np.array(list(item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist))
303+
combos = np.array(list(
304+
item for sublist in itertools.combinations(range(n_qubits), k) for item in sublist
305+
))
312306
combos_list.append(combos)
313307
else:
314308
combos = combos_list[k - 1]
@@ -367,7 +361,6 @@ def multiprocessing_bootstrap(zx_hamiltonian, n_qubits, reheat_tries=0):
367361

368362
return best_theta, best_phi, min_energy
369363

370-
371364
is_charge_update = True
372365
while is_charge_update:
373366
is_charge_update = False
@@ -388,20 +381,21 @@ def multiprocessing_bootstrap(zx_hamiltonian, n_qubits, reheat_tries=0):
388381
jw_term = jordan_wigner(FermionOperator(term=term, coefficient=coeff)) # Transform single term
389382

390383
for pauli_string, jw_coeff in jw_term.terms.items():
391-
if any(p in ("Y") for _, p in pauli_string):
384+
if any(p in ('Y') for _, p in pauli_string):
392385
continue
393386

394387
q = []
395388
b = []
396389
for qubit, op in pauli_string:
397390
# Z/I terms: keep only Z
398-
if op == "I":
391+
if op == 'I':
399392
continue
400393
q.append(qubit)
401-
b.append(op != "Z")
394+
b.append(op != 'Z')
402395

403396
zx_hamiltonian.append((q, b, jw_coeff.real))
404397

398+
405399
# Step 4: Bootstrap!
406400
theta, phi, min_energy = multiprocessing_bootstrap(zx_hamiltonian, n_qubits, 1)
407401

@@ -414,7 +408,7 @@ def multiprocessing_bootstrap(zx_hamiltonian, n_qubits, reheat_tries=0):
414408
for i in range(len(theta)):
415409
b = theta[i]
416410
if theta[i]:
417-
r_electrons += 1 / 2 if phi[i] else 1
411+
r_electrons += 1/2 if phi[i] else 1
418412
if int(r_electrons) != r_electrons:
419413
print("Whoops! We don't have an integer number of charges!")
420414
break
@@ -429,7 +423,7 @@ def multiprocessing_bootstrap(zx_hamiltonian, n_qubits, reheat_tries=0):
429423

430424
if n_electrons != r_electrons or multiplicity != r_multiplicity:
431425
print()
432-
print("Regresssed electron count doesn't match the assumptions!")
426+
print("Regresssed electron count or multiplicity doesn't match the assumptions!")
433427
print("Running again with the natural parameters replacing your assumptions:")
434428
print(f"charge = {r_charge}")
435429
print(f"multiplicity = {r_multiplicity}")

0 commit comments

Comments
 (0)