Quickstart

This code tutorial shows how to estimate a 1-RDM and perform variational optimization

View on QuantumAI Run in Google Colab View source on GitHub Download notebook
try:
    import recirq
except ImportError:
    !pip install --quiet git+https://github.com/quantumlib/ReCirq
import numpy as np
import cirq

from recirq.hfvqe.gradient_hf import rhf_func_generator
from recirq.hfvqe.opdm_functionals import OpdmFunctional
from recirq.hfvqe.analysis import (
    compute_opdm, mcweeny_purification,
    resample_opdm, fidelity_witness,
    fidelity)
from recirq.hfvqe.third_party.higham import fixed_trace_positive_projection
from recirq.hfvqe.molecular_example import make_h6_1_3

Set up the experiment

Generate the input files, set up quantum resources, and set up the OpdmFunctional to make measurements.

rhf_objective, molecule, parameters, obi, tbi = make_h6_1_3()
ansatz, energy, gradient = rhf_func_generator(rhf_objective)

# settings for quantum resources
qubits = [cirq.GridQubit(0, x) for x in range(molecule.n_orbitals)]
sampler = cirq.Simulator(dtype=np.complex128)  # this can be a QuantumEngine

# OpdmFunctional contains an interface for running experiments
opdm_func = OpdmFunctional(qubits=qubits,
                           sampler=sampler,
                           constant=molecule.nuclear_repulsion,
                           one_body_integrals=obi,
                           two_body_integrals=tbi,
                           # only simulate spin-up electrons:
                           num_electrons=molecule.n_electrons // 2,
                           clean_xxyy=True,
                           purification=True
                           )
Optimization terminated successfully.
         Current function value: -2.924060
         Iterations: 7
         Function evaluations: 15
         Gradient evaluations: 15

The displayed text is the output of the gradient based restricted Hartree-Fock. We define the gradient in rhf_objective and use the conjugate-gradient optimizer to optimize the basis rotation parameters. This is equivalent to doing Hartree-Fock theory from the canonical transformation perspective.

Estimate Quantities

Next, we will do the following:

  1. Do measurements for a given set of parameters

  2. Compute 1-RDM, variances, and purification

  3. Compute energy, fidelities, and errorbars

# 1.
# default to 250_000 shots for each circuit.
# 7 circuits total, printed for your viewing pleasure
# return value is a dictionary with circuit results for each permutation
measurement_data = opdm_func.calculate_data(parameters)

# 2.
opdm, var_dict = compute_opdm(measurement_data, return_variance=True)
opdm_pure = mcweeny_purification(opdm)

# 3.
raw_energies = []
raw_fidelity_witness = []
purified_eneriges = []
purified_fidelity_witness = []
purified_fidelity = []
true_unitary = ansatz(parameters)
nocc = molecule.n_electrons // 2
nvirt = molecule.n_orbitals - nocc
initial_fock_state = [1] * nocc + [0] * nvirt

# 1000 repetitions of the measurement
for _ in range(1000):  
    new_opdm = resample_opdm(opdm, var_dict)
    raw_energies.append(opdm_func.energy_from_opdm(new_opdm))
    raw_fidelity_witness.append(
        fidelity_witness(target_unitary=true_unitary,
                         omega=initial_fock_state,
                         measured_opdm=new_opdm)
    )
    # fix positivity and trace of sampled 1-RDM if strictly outside
    # feasible set
    w, v = np.linalg.eigh(new_opdm)
    if len(np.where(w < 0)[0]) > 0:
        new_opdm = fixed_trace_positive_projection(new_opdm, nocc)

    new_opdm_pure = mcweeny_purification(new_opdm)
    purified_eneriges.append(opdm_func.energy_from_opdm(new_opdm_pure))
    purified_fidelity_witness.append(
        fidelity_witness(target_unitary=true_unitary,
                         omega=initial_fock_state,
                         measured_opdm=new_opdm_pure)
    )
    purified_fidelity.append(
        fidelity(target_unitary=true_unitary,
                 measured_opdm=new_opdm_pure)
    )
print("Canonical Hartree-Fock energy ", molecule.hf_energy)
print("True energy ", energy(parameters))
print("Raw energy ", opdm_func.energy_from_opdm(opdm),
      "+- ", np.std(raw_energies))
print("Raw fidelity witness ", np.mean(raw_fidelity_witness).real,
      "+- ", np.std(raw_fidelity_witness))
print("purified energy ", opdm_func.energy_from_opdm(opdm_pure),
      "+- ", np.std(purified_eneriges))
print("Purified fidelity witness ", np.mean(purified_fidelity_witness).real,
      "+- ", np.std(purified_fidelity_witness))
print("Purified fidelity ", np.mean(purified_fidelity).real,
      "+- ", np.std(purified_fidelity))
Canonical Hartree-Fock energy  -2.9240604849733085
True energy  -2.9240604849722245
Raw energy  -2.9263602354432523 +-  0.0015363451987019461
Raw fidelity witness  1.003259209153493 +-  0.0020668449323641034
purified energy  -2.9240552736324372 +-  5.682482913855539e-06
Purified fidelity witness  0.9999779001000263 +-  9.577604854708128e-06
Purified fidelity  0.9999889501815307 +-  4.788772288168814e-06

This prints out the various energies estimated from the 1-RDM along with error bars. Generated from resampling the 1-RDM based on the estimated covariance.

Optimization

We use the sampling functionality to variationally relax the parameters of my ansatz such that the energy is decreased.

For this we will need the augmented Hessian optimizer

The optimizerer code we have takes: rhf_objective object, initial parameters, a function that takes a n x n unitary and returns an opdm maximum iterations, hassian_update which indicates how much of the hessian to use rtol which is the gradient stopping condition.

A natural thing that we will want to save is the variance dictionary of the non-purified 1-RDM. This is accomplished by wrapping the 1-RDM estimation code in another object that keeps track of the variance dictionaries.

from recirq.hfvqe.mfopt import moving_frame_augmented_hessian_optimizer
from recirq.hfvqe.opdm_functionals import RDMGenerator

rdm_generator = RDMGenerator(opdm_func, purification=True)
opdm_generator = rdm_generator.opdm_generator

result = moving_frame_augmented_hessian_optimizer(
    rhf_objective=rhf_objective,
    initial_parameters=parameters + 1.0E-1,
    opdm_aa_measurement_func=opdm_generator,
    verbose=True, delta=0.03,
    max_iter=20,
    hessian_update='diagonal',
    rtol=0.50E-2)
ITERATION NUMBER :  0

 unitary
[[1. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]
Current Energy:  -2.7806354197530094
true energy  -2.780864384381567
dvec
[((0.29128319731732866+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.2891727427400506+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.3270271127271912+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.22930209343550279+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.2356993374069135+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.32688636279586747+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.46972876558292115+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.3129151133221857+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.2899864959054501+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06521848848280869

ITERATION NUMBER :  1

 unitary
[[ 0.98631229 -0.00332115  0.00365105 -0.10006404 -0.08539822 -0.09928762]
 [-0.00332115  0.87717394  0.01627076  0.34088804 -0.09402936 -0.3244132 ]
 [ 0.00365105  0.01627076  0.94020018 -0.096165    0.31064861 -0.09997639]
 [ 0.10006404 -0.34088804  0.096165    0.92807028  0.02868246  0.04896628]
 [ 0.08539822  0.09402936 -0.31064861  0.02868246  0.94154959 -0.00497452]
 [ 0.09928762  0.3244132   0.09997639  0.04896628 -0.00497452  0.93406655]]
Current Energy:  -2.834776508086395
true energy  -2.8350154810386394
dvec
[((0.2387750994576482+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.22983762037661473+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.23855829209594692+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.19754177731565833+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.19302268141601642+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.26301704579948604+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.3449036053947865+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.24834922010415236+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.2193115255115141+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06708509892187875

ITERATION NUMBER :  2

 unitary
[[ 9.91764601e-01  1.43827715e-04  2.75104033e-03 -7.99169649e-02
  -7.00871167e-02 -7.13895095e-02]
 [ 1.51404203e-03  8.78029081e-01  1.36810934e-02  3.60390657e-01
  -7.62453860e-02 -3.05255113e-01]
 [ 9.60251636e-03  1.37720032e-02  9.38024324e-01 -6.68654203e-02
   3.30158166e-01 -7.97062923e-02]
 [ 8.02945713e-02 -3.58431776e-01  6.76276819e-02  9.25456811e-01
   2.22685024e-02  5.94957574e-02]
 [ 6.95114612e-02  7.74972068e-02 -3.29833108e-01  2.52591736e-02
   9.37932972e-01  4.02180187e-03]
 [ 7.09183624e-02  3.07239465e-01  8.09459398e-02  4.63954105e-02
  -7.47507866e-03  9.44358958e-01]]
Current Energy:  -2.8766491740377935
true energy  -2.876662505519228
dvec
[((0.17235814493372098+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.16397433503495898+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.1545521380571233+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.16553562812116632+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.1381481713949234+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.19579450395704553+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.22551971134329468+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.18759975423389597+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.158262025358733+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.07061246459434045

ITERATION NUMBER :  3

 unitary
[[ 9.95873422e-01  1.63211016e-03 -3.55642011e-04 -5.88095487e-02
  -5.29827792e-02 -4.43576321e-02]
 [ 5.36831311e-03  8.77735643e-01  9.30876661e-03  3.81124956e-01
  -5.69345879e-02 -2.84546815e-01]
 [ 1.40994317e-02  1.00447662e-02  9.33818210e-01 -3.78167036e-02
   3.50395805e-01 -5.89621604e-02]
 [ 5.88611490e-02 -3.77238615e-01  3.92743942e-02  9.20709744e-01
   1.47555562e-02  6.89909765e-02]
 [ 5.11694529e-02  5.92958127e-02 -3.49962469e-01  2.00548183e-02
   9.33183707e-01  1.25672991e-02]
 [ 4.38812850e-02  2.89225888e-01  6.22941666e-02  4.17908204e-02
  -1.11579703e-02  9.53242510e-01]]
Current Energy:  -2.90614744541366
true energy  -2.9057097197716164
dvec
[((0.1059302036677815+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.10540949893203874+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.07085612055685699+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.11235842448465388+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.0858829342162121+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.1207655798474051+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.1047900398880364+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.1252556939664271+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.08957047573954932+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.0826223228678802

ITERATION NUMBER :  4

 unitary
[[ 0.99859574  0.00118958 -0.00650146 -0.0371513  -0.03239798 -0.01824878]
 [ 0.0080166   0.87632248  0.00319053  0.40316672 -0.03670391 -0.26094804]
 [ 0.01709244  0.00535936  0.92730126 -0.00911575  0.37198504 -0.03654496]
 [ 0.03614013 -0.39740862  0.01128155  0.91357607  0.00592476  0.07730892]
 [ 0.02846011  0.03993425 -0.3716017   0.01310395  0.92678073  0.02032752]
 [ 0.01827693  0.26925901  0.0429515   0.03464581 -0.01651326  0.96116996]]
Current Energy:  -2.921688353157578
true energy  -2.9217055460248607
dvec
[((0.023814736054062178+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.029583662166747376+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.005965790788958912+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.048055988892962254+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.029947877947184422+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.03066445313376462+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.004099959075946813+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.047237802785452754+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.030038121033093806+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.039811861413646905

ITERATION NUMBER :  5

 unitary
[[ 9.99737448e-01 -3.11900648e-03 -1.73501168e-02 -1.19701126e-02
  -5.16321715e-03  6.65870317e-03]
 [ 9.23788476e-03  8.72509768e-01 -6.38011103e-03  4.31312234e-01
  -1.21336595e-02 -2.28961112e-01]
 [ 1.82577275e-02  3.37962518e-04  9.17082961e-01  1.77699831e-02
   3.97725621e-01 -1.11329337e-02]
 [ 8.62891646e-03 -4.23551490e-01 -1.47712954e-02  9.01661782e-01
  -3.86962057e-03  8.54549181e-02]
 [-2.26787741e-03  1.60280760e-02 -3.97392144e-01  2.33589320e-03
   9.17080015e-01  2.78609374e-02]
 [-5.17212653e-03  2.43031063e-01  2.18724374e-02  2.26114037e-02
  -2.42841212e-02  9.69190257e-01]]
Current Energy:  -2.9236333971709576
true energy  -2.923668201216807
dvec
[((0.00403529731607421+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.0006165223797078845+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.016411866200197696+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.002801052780986992+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.005765729018186041+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.006696803821774927+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.009719281785249612+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.003884435574011122+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.01810126083948533+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.014837405713038616

ITERATION NUMBER :  6

 unitary
[[ 9.99518298e-01 -7.37308205e-03 -2.57810016e-02 -3.40596048e-03
   1.36808502e-02  6.73673414e-03]
 [ 9.01018907e-03  8.70927297e-01 -1.24621496e-02  4.44169896e-01
   1.43106367e-03 -2.09666902e-01]
 [ 1.80362809e-02  5.12560052e-03  9.12665821e-01  1.43161706e-02
   4.08024138e-01  9.32327370e-04]
 [-1.00337490e-03 -4.35651409e-01 -1.06949129e-02  8.95694079e-01
  -2.18957832e-03  8.84295468e-02]
 [-2.32026434e-02  2.67489927e-03 -4.07423159e-01 -4.43576939e-03
   9.12398034e-01  3.11609288e-02]
 [-4.16074987e-03  2.27168888e-01  1.06358864e-02  1.44565324e-02
  -2.91911313e-02  9.73243417e-01]]
Current Energy:  -2.9239236749861184
true energy  -2.9239437805864097
dvec
[((0.005631480699194655+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.0035416549746419557+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.005522299855374918+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.0012051810363024155+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.008944455086606138+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.001993068206962767+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.0009604411339380031+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.0011609598581457797+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.009947490732409647+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.007874950049744664

ITERATION NUMBER :  7

 unitary
[[ 9.99551381e-01 -8.83758754e-03 -2.53218914e-02 -1.99689787e-03
   1.26780111e-02  3.60784548e-03]
 [ 8.94740686e-03  8.70719242e-01 -1.36784264e-02  4.43868033e-01
   4.18962448e-03 -2.11055592e-01]
 [ 1.80942876e-02  1.18178075e-02  9.13817360e-01  4.17059744e-03
   4.05466043e-01  7.11779866e-03]
 [-2.41367363e-03 -4.35283863e-01 -1.88722857e-04  8.95935607e-01
   2.45247670e-03  8.84051495e-02]
 [-2.19733851e-02  9.90929321e-05 -4.05087412e-01 -5.73484890e-03
   9.13442423e-01  3.18025229e-02]
 [-9.60440767e-04  2.28381671e-01  3.69977261e-03  1.50428352e-02
  -3.21858379e-02  9.72915714e-01]]
Current Energy:  -2.924015623879185
true energy  -2.9240147738686644
dvec
[((-0.0002962899955560147+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.0006892271238582715+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.0021216651016876006+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.005505192961154141+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.004990752725768814+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.003706856217531973+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.002825800011310011+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.000314173227668163+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.00587467584098354+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.004833672445300825
Finished Optimization

Each interation prints out a variety of information that the user might find useful. Watching energies go down is known to be one of the best forms of entertainment during a shelter-in-place order.

After the optimization we can print the energy as a function of iteration number to see close the energy gets to the true minium.

import matplotlib.pyplot as plt

plt.semilogy(range(len(result.func_vals)),
             np.abs(np.array(result.func_vals) - energy(parameters)),
             'C0o-')
plt.xlabel("Optimization Iterations",  fontsize=18)
plt.ylabel(r"$|E  - E^{*}|$", fontsize=18)
plt.tight_layout()
plt.show()

png