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:
Do measurements for a given set of parameters
Compute 1-RDM, variances, and purification
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.9242645210470766 +- 0.0015331783873733394 Raw fidelity witness 1.0004357916239772 +- 0.0020722540501955625 purified energy -2.9240569756732837 +- 5.408889557605887e-06 Purified fidelity witness 0.9999841069559398 +- 7.076215230560281e-06 Purified fidelity 0.99999205387166 +- 3.5380733600844085e-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.7813503484821815 true energy -2.780864384381567 dvec [((0.2872200618653983+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.2913547739465195+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.3261941609925103+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.22796038419138498+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.2329631151953604+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.3266748511664813+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.4678728414708997+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.3140908325055142+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.2916023326656878+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.06525107996451014 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.8343785619901363 true energy -2.8350453474180544 dvec [((0.23282206463480448+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.2300698637311349+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.23974936941478692+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.2014285409481783+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.18916324500905712+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.2662101322840197+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.34469979684024704+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.25142035893935893+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.22379167707722167+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.06687642699018448 ITERATION NUMBER : 2 unitary [[ 9.91740501e-01 2.15049535e-04 2.74062046e-03 -8.01436669e-02 -7.01358936e-02 -7.14224830e-02] [ 1.53370899e-03 8.77990122e-01 1.37592300e-02 3.60571098e-01 -7.63943555e-02 -3.05113196e-01] [ 9.62646560e-03 1.38330372e-02 9.38023562e-01 -6.68562432e-02 3.30201607e-01 -7.95293423e-02] [ 8.05329957e-02 -3.58602751e-01 6.76248815e-02 9.25367741e-01 2.22674643e-02 5.95322023e-02] [ 6.95547776e-02 7.76377698e-02 -3.29876561e-01 2.53159708e-02 9.37901549e-01 3.96851836e-03] [ 7.09389010e-02 3.07112991e-01 8.07669411e-02 4.63612424e-02 -7.52338580e-03 9.44415173e-01]] Current Energy: -2.8771792849264113 true energy -2.8765852349186964 dvec [((0.17769218486827956+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.165364355372025+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.14902147863822762+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.16426096639640592+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.14151447674313125+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.19500567129310475+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.22279034161981953+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.18643283530807797+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.1472267473813937+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.07261471036131459 ITERATION NUMBER : 3 unitary [[ 9.95821452e-01 1.90871053e-03 -5.04711691e-04 -5.96417708e-02 -5.28235543e-02 -4.45900027e-02] [ 5.37063464e-03 8.77744033e-01 9.56423845e-03 3.81210630e-01 -5.75863128e-02 -2.84266366e-01] [ 1.41673703e-02 1.02069218e-02 9.33778335e-01 -3.77908762e-02 3.50575427e-01 -5.84965399e-02] [ 5.97486303e-02 -3.77329898e-01 3.92723169e-02 9.20619069e-01 1.47117087e-02 6.89494648e-02] [ 5.09755910e-02 5.98942179e-02 -3.50144630e-01 2.03392904e-02 9.33084197e-01 1.23735083e-02] [ 4.40643526e-02 2.88950548e-01 6.18281085e-02 4.17133855e-02 -1.13050813e-02 9.53349562e-01]] Current Energy: -2.9063424789077072 true energy -2.906333131489356 dvec [((0.10742178758184366+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.10337847902232336+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.07134856843539368+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.10656545062472542+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.08822480923982956+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.11627141792108456+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.10947580704741419+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.12524569113006917+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.09087416724387855+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.08253871020242336 ITERATION NUMBER : 4 unitary [[ 0.99865202 0.00104148 -0.00684192 -0.03651749 -0.03159371 -0.01773554] [ 0.008133 0.87615905 0.00298992 0.40426139 -0.0360593 -0.25988976] [ 0.01715592 0.00532646 0.92692443 -0.00911587 0.37289932 -0.03676136] [ 0.03544991 -0.39843099 0.01128901 0.91313416 0.00596948 0.07758203] [ 0.02755297 0.03929866 -0.37251004 0.01288014 0.92646879 0.02055224] [ 0.017827 0.26837328 0.04317605 0.03429586 -0.01635855 0.96143123]] Current Energy: -2.9216598392432105 true energy -2.921804356661534 dvec [((0.021656529277991637+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.029711531006416547+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((-0.007363527374325099+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.04847951669175547+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.02704399718704943+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.031636186305677744+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((-0.00518258060726222+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.04309516845385479+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.034977440439628804+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.03940512471009999 ITERATION NUMBER : 5 unitary [[ 9.99737767e-01 -3.27329927e-03 -1.71034652e-02 -1.09728883e-02 -5.74456233e-03 8.23107830e-03] [ 9.29352305e-03 8.72509326e-01 -6.90483075e-03 4.31937385e-01 -1.07436518e-02 -2.27833488e-01] [ 1.82630998e-02 1.89447908e-04 9.17088717e-01 1.80036049e-02 3.97706899e-01 -1.09453321e-02] [ 7.52059362e-03 -4.24137645e-01 -1.50078497e-02 9.01381988e-01 -3.98569269e-03 8.55575197e-02] [-1.69406871e-03 1.46912102e-02 -3.97378209e-01 1.68989119e-03 9.17100965e-01 2.81925940e-02] [-6.71255183e-03 2.42091204e-01 2.17578194e-02 2.22084978e-02 -2.43049613e-02 9.69427377e-01]] Current Energy: -2.9236606958044185 true energy -2.923692682495246 dvec [((-0.00418925651014337+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((-0.002345287084930675+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((-0.015994787511825655+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.000842834499527545+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.004995478918734319+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((-0.010950691543959612+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((-0.022021811917874987+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.0036618884484373865+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.012948209178358545+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.015512518730486707 ITERATION NUMBER : 6 unitary [[ 0.99952132 -0.00725966 -0.02560427 -0.00320374 0.01328317 0.007883 ] [ 0.00906738 0.87048888 -0.01242693 0.44481167 0.00148423 -0.21012576] [ 0.01801315 0.00586425 0.91255077 0.01372162 0.40828269 0.00303351] [-0.00122959 -0.43628773 -0.01007062 0.89538139 -0.00192039 0.08853563] [-0.02280928 0.00264573 -0.4077562 -0.00444863 0.91226066 0.03111759] [-0.00535384 0.22761377 0.00863446 0.01471067 -0.03005652 0.9731233 ]] Current Energy: -2.923921307680248 true energy -2.9239161992105287 dvec [((0.012724838975715524+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.002877489261944949+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((-0.0024267378398793666+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.0033879377589322683+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.010055730306234962+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.00307576409955565+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.007751376006523919+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((-0.005963003716088447+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.009109411147024984+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.00935662124916462 ITERATION NUMBER : 7 unitary [[ 9.99525847e-01 -8.23322705e-03 -2.57893795e-02 -4.82080082e-03 1.38486375e-02 4.23263213e-04] [ 9.00613981e-03 8.71303493e-01 -1.34593252e-02 4.43811197e-01 3.79478754e-03 -2.08770642e-01] [ 1.81032264e-02 1.19471619e-02 9.14331365e-01 3.80207344e-03 4.04304880e-01 7.12750974e-03] [ 6.64511935e-04 -4.35221215e-01 8.75850657e-05 8.95972584e-01 2.64943520e-03 8.83637635e-02] [-2.31208597e-02 4.39661912e-04 -4.03904923e-01 -5.55650775e-03 9.13942948e-01 3.16774230e-02] [ 2.05646953e-03 2.26278597e-01 3.56579408e-03 1.40064460e-02 -3.21348290e-02 9.73422942e-01]] Current Energy: -2.924020500329758 true energy -2.924008847952976 dvec [((-0.0030487772802942795+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.002435442917437469+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((-0.0060144537082670885+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.0033964131284667624+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((-0.0005080572811470333+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.002102691299483511+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((-0.0067159703375135466+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.0013765454253000242+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.0038176941134737585+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.0055934151346724486 ITERATION NUMBER : 8 unitary [[ 9.99550961e-01 -9.85393588e-03 -2.51872247e-02 -1.95948234e-04 1.24379183e-02 3.41160877e-03] [ 8.88595846e-03 8.70172610e-01 -1.55540677e-02 4.44975395e-01 8.60025283e-03 -2.10717044e-01] [ 1.80828382e-02 1.35345062e-02 9.13863980e-01 2.33659019e-03 4.05244512e-01 1.06715058e-02] [-4.42443500e-03 -4.36351420e-01 1.78104086e-03 8.95379205e-01 3.25713096e-03 8.86575477e-02] [-2.17240796e-02 -4.09211141e-03 -4.04935923e-01 -7.86226094e-03 9.13456914e-01 3.27546639e-02] [-6.44258172e-04 2.28254329e-01 1.66259550e-04 1.50220918e-02 -3.36760542e-02 9.72902759e-01]] Current Energy: -2.9240319885881516 true energy -2.924036129510131 dvec [((-0.0002777387200173487+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((-0.002485112291374464+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((-0.0006998188611801676+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.002609861326833659+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.004906551922227834+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((-0.0017163779608479202+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((-0.002043015004162687+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.00189040168145313+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.004925149199522375+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.0038620899606095624 Finished Optimization
Each iteration 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()