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.9237346568316736 +-  0.0015586566195406417
Raw fidelity witness  1.000305408130552 +-  0.0020855114094798454
purified energy  -2.924054711717596 +-  6.644501021861774e-06
Purified fidelity witness  0.9999801444686149 +-  8.73067758104101e-06
Purified fidelity  0.9999900724708685 +-  4.365289464024962e-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.7809359489121874
true energy  -2.780864384381567
dvec
[((0.2895658662812913+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.28986035252044984+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.3259023176637458+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.22853517742245463+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.2368346658203101+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.3250804375317689+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.47018998298513104+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.3134035760131727+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.2891539300615975+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06539028348445923

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.836238750013065
true energy  -2.835144918741025
dvec
[((0.23697789000768044+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.22616237637552244+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.23544421226067283+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.19732085372177688+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.18677005023203383+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.2607428235206569+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.3473323979864978+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.2470056083504256+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.21871229898895728+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06726134850272376

ITERATION NUMBER :  2

 unitary
[[ 9.91771919e-01  2.05723259e-04  2.75685233e-03 -7.99571110e-02
  -7.00790160e-02 -7.12503341e-02]
 [ 1.54714396e-03  8.78030702e-01  1.36441627e-02  3.60515543e-01
  -7.60757086e-02 -3.05146783e-01]
 [ 9.60173548e-03  1.37756153e-02  9.38039443e-01 -6.68663551e-02
   3.30123146e-01 -7.96720881e-02]
 [ 8.03436498e-02 -3.58542677e-01  6.76260157e-02  9.25407501e-01
   2.22740789e-02  5.95280619e-02]
 [ 6.95010712e-02  7.73415060e-02 -3.29800441e-01  2.51940060e-02
   9.37959614e-01  4.07273721e-03]
 [ 7.07698580e-02  3.07144456e-01  8.09112575e-02  4.63736179e-02
  -7.46692955e-03  9.44405110e-01]]
Current Energy:  -2.8766815733945927
true energy  -2.8768705829672676
dvec
[((0.17647766397877923+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.1689277293782323+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.15258657481645096+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.1605160336699904+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.1404872017284371+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.19305284798910147+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.22370690893377673+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.1922121880741578+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.15178468187480856+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.07158297031870606

ITERATION NUMBER :  3

 unitary
[[ 9.95909272e-01  1.81908218e-03 -3.69201502e-04 -5.87752980e-02
  -5.28313941e-02 -4.37674041e-02]
 [ 5.36529953e-03  8.77770867e-01  9.42860471e-03  3.81160627e-01
  -5.71941122e-02 -2.84334362e-01]
 [ 1.41009609e-02  1.00924312e-02  9.33829415e-01 -3.78108690e-02
   3.50395628e-01 -5.87807075e-02]
 [ 5.88547558e-02 -3.77273998e-01  3.92724857e-02  9.20696102e-01
   1.47582828e-02  6.89855106e-02]
 [ 5.10040014e-02  5.95367254e-02 -3.49961738e-01  2.01700123e-02
   9.33176345e-01  1.24832817e-02]
 [ 4.32644068e-02  2.89020497e-01  6.21131431e-02  4.17640587e-02
  -1.11662016e-02  9.53345887e-01]]
Current Energy:  -2.906100762407193
true energy  -2.9061460931631906
dvec
[((0.10442456354156589+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.10229828954978942+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.07022192349478655+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.11402686165766934+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.08764670907629342+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.11889425246589244+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.10442974316374937+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.12469085571315565+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.09216517843907618+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.08130707194276339

ITERATION NUMBER :  4

 unitary
[[ 0.99863884  0.00105318 -0.00636962 -0.03631749 -0.03252196 -0.01738248]
 [ 0.00809059  0.87623316  0.00312503  0.40416114 -0.03629198 -0.25976316]
 [ 0.01705046  0.00523299  0.92727291 -0.00914066  0.37202773 -0.03686013]
 [ 0.03524787 -0.39832623  0.0112743   0.91318828  0.00600928  0.07757406]
 [ 0.02859393  0.03954426 -0.37164036  0.01290171  0.92677904  0.02040192]
 [ 0.01744331  0.26825254  0.04325454  0.03423399 -0.01628116  0.96147195]]
Current Energy:  -2.9215684594210596
true energy  -2.921724359406835
dvec
[((0.025691790836803438+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.03540233013693525+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.005711661922832114+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.04624533412427484+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.02961431353782898+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.03630654190644683+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.007771994607807492+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.04399145714004518+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.031183715635133967+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.04087525337061903

ITERATION NUMBER :  5

 unitary
[[ 9.99738056e-01 -3.09667954e-03 -1.72098909e-02 -1.18420846e-02
  -5.26725261e-03  7.07603588e-03]
 [ 9.25402782e-03  8.72748014e-01 -6.51534225e-03  4.31150801e-01
  -1.15173783e-02 -2.28383787e-01]
 [ 1.81684151e-02  4.82319098e-04  9.17407336e-01  1.71841691e-02
   3.97005457e-01 -1.11726078e-02]
 [ 8.50178145e-03 -4.23388403e-01 -1.42393771e-02  9.01758078e-01
  -3.59994160e-03  8.53617590e-02]
 [-2.12053213e-03  1.54685604e-02 -3.96667544e-01  2.03803817e-03
   9.17402384e-01  2.79248728e-02]
 [-5.59586369e-03  2.42495497e-01  2.18460311e-02  2.23980327e-02
  -2.42111423e-02  9.69329418e-01]]
Current Energy:  -2.923676243360466
true energy  -2.9236803015340427
dvec
[((0.0008001774085679987+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.004138716347129395+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.013391153629169388+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.0027234349867337852+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.003739911185687647+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.010902959262630268+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.010604427535283952+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.0005952767742873066+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.020669969667212466+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.014158082150824137

ITERATION NUMBER :  6

 unitary
[[ 0.99954126 -0.00799675 -0.02536968 -0.00263619  0.01292479  0.00597555]
 [ 0.00904765  0.86968703 -0.01257082  0.44638993  0.00188864 -0.21008819]
 [ 0.01792999  0.00531616  0.912129    0.01390442  0.4092378   0.00142373]
 [-0.00191052 -0.43781684 -0.01029468  0.89459234 -0.00198798  0.08892341]
 [-0.02234411  0.00229441 -0.40866864 -0.00470214  0.91186083  0.03119521]
 [-0.00331988  0.22773539  0.01014883  0.01477087 -0.02932041  0.97311084]]
Current Energy:  -2.923962545392556
true energy  -2.9239443005757098
dvec
[((0.0023563726382570555+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.0012901941701811137+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.0067081731929416605+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.004294453309659722+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.008156730750115656+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.0024548634833415905+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.004674020804635102+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.0013581110314040135+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.004307122818366581+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.0071459398094569915

ITERATION NUMBER :  7

 unitary
[[ 9.99571921e-01 -8.96358783e-03 -2.49157650e-02 -2.39566453e-03
   1.19639941e-02  2.44087431e-03]
 [ 9.00927651e-03  8.70610366e-01 -1.33426235e-02  4.44604658e-01
   3.64348693e-03 -2.09980525e-01]
 [ 1.80251567e-02  1.13496289e-02  9.13927114e-01  5.66360602e-03
   4.05184332e-01  8.78015810e-03]
 [-2.04537657e-03 -4.36006809e-01 -1.75670373e-03  8.95570158e-01
   1.82934438e-03  8.85524669e-02]
 [-2.11115405e-02  6.39373633e-04 -4.04882988e-01 -5.52442988e-03
   9.13560555e-01  3.16267690e-02]
 [ 1.46426810e-04  2.27433693e-01  2.25601950e-03  1.45758562e-02
  -3.27568391e-02  9.73130689e-01]]
Current Energy:  -2.924020197936495
true energy  -2.9240161529620803
dvec
[((0.001803299654991615+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.0028604627712461306+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.004840624073542665+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.000947808914216957+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.003785745116355871+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.005456282768761894+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.0004412314960055444+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.0009000556306812818+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.00326025820747627+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.004907435187931638
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