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))
/tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) /tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) /tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) /tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) /tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) /tmpfs/tmp/ipykernel_53802/2348574811.py:8: DeprecationWarning: `alltrue` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `all` instead. opdm, var_dict = compute_opdm(measurement_data, return_variance=True) Canonical Hartree-Fock energy -2.9240604849733085 True energy -2.924060484972228 Raw energy -2.92501520484372 +- 0.0015373229226962215 Raw fidelity witness 1.0006341684295428 +- 0.0020463402130866592 purified energy -2.9240507428762026 +- 7.435703475417347e-06 Purified fidelity witness 0.9999704958323998 +- 1.1666531332883893e-05 Purified fidelity 0.9999852483461336 +- 5.833220498053128e-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.8257703726328653 true energy -2.826271292768161 dvec [((0.13052845830280668+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.14825125196090796+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.1108751920895707+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.3326724727006415+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.14273685366077007+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.2642189806752524+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.2039164727085938+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.22285984279218796+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.23572151182955378+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.05232747512342904 ITERATION NUMBER : 1 unitary [[ 0.98389642 0.01872322 -0.03839118 -0.09592923 -0.10702297 -0.09729877] [ 0.01872322 0.92317564 -0.00126495 0.34627269 -0.09792622 0.13379184] [-0.03839118 -0.00126495 0.85968895 -0.09412702 -0.4916874 -0.09403736] [ 0.09592923 -0.34627269 0.09412702 0.92774284 -0.01313115 -0.03396633] [ 0.10702297 0.09792622 0.4916874 -0.01313115 0.85816974 -0.02392281] [ 0.09729877 -0.13379184 0.09403736 -0.03396633 -0.02392281 0.98084844]] Current Energy: -2.855095614085176 true energy -2.8557698522628137 dvec [((0.13127015832465797+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.13397642621381423+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.08766582947656022+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.25146785258457394+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.1273975183902599+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.19686274342714863+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.1732242082785144+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.18896804966109476+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.192837679676448+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.056620805454011394 ITERATION NUMBER : 2 unitary [[ 0.9887763 0.0161957 -0.02188607 -0.08795138 -0.08138875 -0.08497388] [ 0.02247074 0.91787116 0.00753748 0.35701191 -0.08686051 0.14815038] [-0.03344685 -0.00695927 0.87259474 -0.08232489 -0.47421553 -0.07585206] [ 0.08674818 -0.35776151 0.08244825 0.92548931 -0.0044384 -0.0336686 ] [ 0.07789973 0.08530673 0.47458971 -0.01319609 0.87223363 -0.02129219] [ 0.08428638 -0.14809211 0.07752094 -0.03640126 -0.01181304 0.98157549]] Current Energy: -2.880426131471662 true energy -2.8808873880187624 dvec [((0.11807293243739292+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.11089785584085099+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.06855085592487498+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.16638271715129294+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.1121468405583266+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.13136353726192995+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.1431725837670233+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.15354585910724966+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.14400335897362548+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.06597552339672372 ITERATION NUMBER : 3 unitary [[ 9.92919012e-01 1.13087318e-02 -5.85189939e-03 -7.68259228e-02 -5.51108171e-02 -7.07833293e-02] [ 2.60779303e-02 9.10774292e-01 1.74176369e-02 3.70240711e-01 -7.27947936e-02 1.64710322e-01] [-2.93447045e-02 -1.40947770e-02 8.84630909e-01 -6.97640278e-02 -4.56483826e-01 -5.58916577e-02] [ 7.38173343e-02 -3.71775121e-01 6.94480227e-02 9.22133428e-01 4.56088606e-03 -3.40642221e-02] [ 4.84143819e-02 7.03679535e-02 4.56954569e-01 -1.50410566e-02 8.85022662e-01 -2.01389034e-02] [ 6.91210176e-02 -1.64302312e-01 5.88220202e-02 -3.99534782e-02 5.64735019e-04 9.81412455e-01]] Current Energy: -2.902053151265057 true energy -2.9018249264281444 dvec [((0.10421907863588206+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.084414094745878+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.05672182343118644+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.0778610918252007+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.08710578197187221+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.06893840700808823+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.09454937831215361+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.1062696596761561+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.08116943499708328+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.07573605525169666 ITERATION NUMBER : 4 unitary [[ 0.99628725 0.00265284 0.00965029 -0.06121977 -0.02805745 -0.05269217] [ 0.02943726 0.90063333 0.02963318 0.38716221 -0.05268228 0.18559343] [-0.02605488 -0.02395245 0.89606008 -0.05456753 -0.43788796 -0.03317036] [ 0.05540262 -0.38955756 0.05287942 0.91700453 0.01464465 -0.03560119] [ 0.01851882 0.05045815 0.43813714 -0.0200965 0.89682276 -0.02125915] [ 0.04953691 -0.18433047 0.03667006 -0.0455935 0.01363704 0.97977495]] Current Energy: -2.916189733080895 true energy -2.9162414169114483 dvec [((0.0633153928503707+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.0428224578927022+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.03873487664435642+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.006523741098973178+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.06261988396791636+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.017727719360230293+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.05841029498412897+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.051976928084578336+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.03575204899507642+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.05804235479722542 ITERATION NUMBER : 5 unitary [[ 0.99846169 -0.01219629 0.02152844 -0.03703279 -0.00568641 -0.032531 ] [ 0.03175716 0.88584982 0.04427515 0.40920957 -0.02454772 0.21034789] [-0.02397513 -0.03825841 0.90551752 -0.03242796 -0.42048501 -0.01184447] [ 0.02695827 -0.41224106 0.02802095 0.90899671 0.02731218 -0.03904382] [-0.00555961 0.02432866 0.42026426 -0.03009228 0.90619253 -0.0257822 ] [ 0.02707895 -0.20766096 0.01438879 -0.05425835 0.02503162 0.97589245]] Current Energy: -2.922053735356245 true energy -2.9219860276116263 dvec [((0.032102328872922865+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.014675621982090686+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.025719785896816512+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.010547600688584385+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.02942367969951314+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.010628071574976278+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.026143043091140593+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.011471416216013497+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.019695848228153595+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.031100976965021385 ITERATION NUMBER : 6 unitary [[ 9.99162022e-01 -2.62566105e-02 2.09419844e-02 -1.64289026e-02 -6.79350638e-03 -1.52058229e-02] [ 3.22253416e-02 8.74324162e-01 5.72144312e-02 4.24107612e-01 3.81789645e-03 2.26635065e-01] [-2.33853547e-02 -5.16185680e-02 9.08661568e-01 -1.09442058e-02 -4.13524803e-01 5.15028142e-04] [ 3.13816858e-03 -4.27233194e-01 4.00713440e-03 9.02329387e-01 3.80237590e-02 -4.24471922e-02] [-3.71612427e-03 -1.84377840e-03 4.13044612e-01 -4.25272846e-02 9.09131923e-01 -3.23653607e-02] [ 8.13933587e-03 -2.22871648e-01 4.34068610e-04 -6.11241096e-02 3.11424177e-02 9.72396927e-01]] Current Energy: -2.923479307939994 true energy -2.9234705120667193 dvec [((0.007585969834950889+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.005284359873986094+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.01581042497743668+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.00841441126408252+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.02063321719895246+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.005796207238436518+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.014879599626364188+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.002806268292484204+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.0056887153869194865+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.01734268400859142 ITERATION NUMBER : 7 unitary [[ 9.99161048e-01 -3.37685328e-02 1.89788263e-02 -5.14412192e-03 -1.01132101e-02 -6.92466806e-03] [ 3.21943880e-02 8.70209996e-01 6.33937736e-02 4.29435431e-01 1.80432028e-02 2.30084695e-01] [-2.34191602e-02 -6.11604515e-02 9.10217931e-01 4.45896886e-03 -4.08824480e-01 7.54468345e-03] [-9.56651018e-03 -4.31932038e-01 -1.29188750e-02 8.99545129e-01 4.54021194e-02 -4.39716708e-02] [ 4.49501319e-04 -1.49721123e-02 4.08536562e-01 -4.90314182e-02 9.10593824e-01 -3.58931514e-02] [-7.37455667e-04 -2.25973889e-01 -7.43851702e-03 -6.28756504e-02 3.45289033e-02 9.71459898e-01]] Current Energy: -2.9238329383746113 true energy -2.9238774828930247 dvec [((0.0028171235039788+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.00511436195887871+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.01082614388564845+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.0008848023810898668+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.009907155784615271+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.0005536510258484506+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.006663547804356291+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.0028048017629660055+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.009704833031344272+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.010610265461652287 ITERATION NUMBER : 8 unitary [[ 0.9991032 -0.03616679 0.01740076 -0.00253765 -0.01309911 -0.00198657] [ 0.03218561 0.86866449 0.06767123 0.43109238 0.02789225 0.23062541] [-0.02354333 -0.06650095 0.91089745 0.01409782 -0.40620766 0.00926092] [-0.01243547 -0.43331903 -0.02316832 0.8983676 0.04986765 -0.04463262] [ 0.00378858 -0.02420812 0.40589645 -0.05364335 0.91120643 -0.03837859] [-0.00579648 -0.22658369 -0.00974472 -0.06334206 0.03552224 0.9712143 ]] Current Energy: -2.9239730118632004 true energy -2.924005494926797 dvec [((0.009704707465789702+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((-0.001043527188637039+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.0005223455949390611+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((-0.004275640126944319+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.006800136393422565+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.0016210601891616938+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((-0.004021272549110472+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((-0.003407825030075501+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.009981600951595657+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.007230578241517629 ITERATION NUMBER : 9 unitary [[ 9.99074143e-01 -3.71854219e-02 1.74992111e-02 -1.68823963e-03 -1.26095719e-02 1.62843389e-04] [ 3.21644597e-02 8.67224446e-01 6.96618127e-02 4.32937827e-01 3.27031969e-02 2.31365033e-01] [-2.36677969e-02 -7.06847489e-02 9.10735281e-01 2.05917320e-02 -4.05483320e-01 1.28065793e-02] [-1.34663583e-02 -4.34881427e-01 -3.02144115e-02 8.97183969e-01 5.28642603e-02 -4.52789923e-02] [ 3.24730420e-03 -2.86054435e-02 4.05347802e-01 -5.58491182e-02 9.11142332e-01 -3.95584487e-02] [-8.01567850e-03 -2.27161935e-01 -1.35095277e-02 -6.38730767e-02 3.71456394e-02 9.70922823e-01]] Current Energy: -2.9240290090989887 true energy -2.924029347673618 dvec [((-0.0040178588753819015+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((-0.0005234877030230178+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.004032099195073885+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.002255377248345604+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((-0.0009261581191203437+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((0.00034352930834646855+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.00836128106951236+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((0.004833816908515462+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((0.003635070721855321+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.005066166947845233 ITERATION NUMBER : 10 unitary [[ 0.99901646 -0.03851327 0.01665498 0.00200287 -0.01413551 -0.00127473] [ 0.03219615 0.86763363 0.07111747 0.43237403 0.03582262 0.22997181] [-0.02370079 -0.07179524 0.91091059 0.02062148 -0.40474582 0.01676835] [-0.0173377 -0.43427398 -0.03057767 0.89739226 0.05308116 -0.04516683] [ 0.00499133 -0.03167367 0.40457107 -0.05737898 0.91124718 -0.04037761] [-0.00650236 -0.22578075 -0.01714822 -0.06338942 0.03884225 0.97116413]] Current Energy: -2.924046294418622 true energy -2.9240470326835015 dvec [((-0.001925360926970953+0j), -1.0 [0^ 6] + -1.0 [1^ 7] + 1.0 [6^ 0] + 1.0 [7^ 1]), ((0.0041881919975730875+0j), -1.0 [2^ 6] + -1.0 [3^ 7] + 1.0 [6^ 2] + 1.0 [7^ 3]), ((0.001617012238533187+0j), -1.0 [4^ 6] + -1.0 [5^ 7] + 1.0 [6^ 4] + 1.0 [7^ 5]), ((0.0014886621759226584+0j), -1.0 [0^ 8] + -1.0 [1^ 9] + 1.0 [8^ 0] + 1.0 [9^ 1]), ((0.0034660030116082027+0j), -1.0 [2^ 8] + -1.0 [3^ 9] + 1.0 [8^ 2] + 1.0 [9^ 3]), ((-0.0028079616848067923+0j), -1.0 [4^ 8] + -1.0 [5^ 9] + 1.0 [8^ 4] + 1.0 [9^ 5]), ((0.0032070738468375965+0j), -1.0 [0^ 10] + -1.0 [1^ 11] + 1.0 [10^ 0] + 1.0 [11^ 1]), ((-0.002209713006347064+0j), -1.0 [2^ 10] + -1.0 [3^ 11] + 1.0 [10^ 2] + 1.0 [11^ 3]), ((-0.0031291478798688922+0j), -1.0 [4^ 10] + -1.0 [5^ 11] + 1.0 [10^ 4] + 1.0 [11^ 5])] New fr values norm 0.0037680501792427797 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()