Using the library(odd number of qubits)

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


In [1]:
# Import library functions and define a helper function
import numpy as np
import cirq

from openfermioncirq.experiments.hfvqe.gradient_hf import rhf_func_generator
from openfermioncirq.experiments.hfvqe.opdm_functionals import OpdmFunctional
from openfermioncirq.experiments.hfvqe.analysis import (compute_opdm,
                            mcweeny_purification,
                            resample_opdm,
                            fidelity_witness,
                            fidelity)
from openfermioncirq.experiments.hfvqe.third_party.higham import fixed_trace_positive_projection
from openfermioncirq.experiments.hfvqe.molecular_example_odd_qubits import make_h3_2_5

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


In [2]:
rhf_objective, molecule, parameters, obi, tbi = make_h3_2_5()
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,
                           num_electrons=molecule.n_electrons // 2,  # only simulate spin-up electrons
                           clean_xxyy=True,
                           purification=True
                           )


Optimization terminated successfully.
         Current function value: -0.806205
         Iterations: 2
         Function evaluations: 7
         Gradient evaluations: 7

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.

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


In [3]:
# 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
for _ in range(1000):  # 1000 repetitions of the measurement
    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('\n\n\n\n')
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  -0.8062049864800688
True energy  -0.8062049864970051
Raw energy  -0.8067272958108533 +-  0.00030356622331861833
Raw fidelity witness  1.0018126839180024 +-  0.0010710794044197392
purified energy  -0.8062047900762465 +-  1.3877788570410645e-06
Purified fidelity witness  0.9999969290687779 +-  3.1373627657046185e-06
Purified fidelity  0.9999984646041123 +-  1.5686788163360926e-06

This should print 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.


In [4]:
from openfermioncirq.experiments.hfvqe.mfopt import moving_frame_augmented_hessian_optimizer
from openfermioncirq.experiments.hfvqe.opdm_functionals import RDMGenerator
import matplotlib.pyplot as plt
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 + 5.0E-1 ,
    opdm_aa_measurement_func=opdm_generator,
    verbose=True, delta=0.03,
    max_iter=120,
    hessian_update='diagonal',
    rtol=0.050E-2)


ITERATION NUMBER :  0

 unitary
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Current Energy:  -0.34388017269864857
true energy  -0.45436924460009553
dvec
[((0.31798621005587846+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.22533657264296178+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.030128430811863286

ITERATION NUMBER :  1

 unitary
[[ 0.37140703 -0.39002425 -0.84257813]
 [ 0.39002425  0.88907822 -0.23962682]
 [ 0.84257813 -0.23962682  0.48232881]]
Current Energy:  -0.35340495365392377
true energy  -0.4657672990955405
dvec
[((0.3567599843213132+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.26613671383110404+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.030243490161151607

ITERATION NUMBER :  2

 unitary
[[ 0.39759567 -0.39456467 -0.82839387]
 [ 0.38895567  0.89016845 -0.2373049 ]
 [ 0.83104222 -0.22785709  0.50739529]]
Current Energy:  -0.3640083540633037
true energy  -0.4787483342065876
dvec
[((0.3932226313223952+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.3046321543181286+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.030429071254522457

ITERATION NUMBER :  3

 unitary
[[ 0.42383078 -0.3978101  -0.81370424]
 [ 0.38738253  0.89168595 -0.23416007]
 [ 0.81871988 -0.21597057  0.53202864]]
Current Energy:  -0.37600037581911905
true energy  -0.49335502420309885
dvec
[((0.42872161514366774+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.34219612473571775+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.030734604993085408

ITERATION NUMBER :  4

 unitary
[[ 0.45016463 -0.3995643  -0.79856132]
 [ 0.38515717  0.89371622 -0.23005493]
 [ 0.80560894 -0.20400903  0.55621449]]
Current Energy:  -0.39031789566903496
true energy  -0.5096713478988968
dvec
[((0.46255658006283207+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.3777859502134872+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0311947211652605

ITERATION NUMBER :  5

 unitary
[[ 0.47668875 -0.39952626 -0.78303679]
 [ 0.38206146  0.8963852  -0.22477236]
 [ 0.79170504 -0.19202173  0.57994032]]
Current Energy:  -0.407401361686787
true energy  -0.5277414308197705
dvec
[((0.4957978448083651+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.4125909011068826+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.031914206236235636

ITERATION NUMBER :  6

 unitary
[[ 0.50347041 -0.39742832 -0.76718204]
 [ 0.37787071  0.89979265 -0.21814427]
 [ 0.77700146 -0.18006644  0.60319549]]
Current Energy:  -0.42710984216214953
true energy  -0.5476692830926745
dvec
[((0.5257204389468734+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.44359854072069743+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03289725382240359

ITERATION NUMBER :  7

 unitary
[[ 0.53063312 -0.39283306 -0.75107301]
 [ 0.37224133  0.90409461 -0.2098793 ]
 [ 0.76148859 -0.16821151  0.62597125]]
Current Energy:  -0.44970105864408005
true energy  -0.5694079541242683
dvec
[((0.552286963385154+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.47071012003553203+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03422857503987146

ITERATION NUMBER :  8

 unitary
[[ 0.55821062 -0.38546357 -0.73472358]
 [ 0.36489139  0.90934829 -0.19984984]
 [ 0.74515447 -0.15653601  0.64826021]]
Current Energy:  -0.4760695077350927
true energy  -0.5928798871981653
dvec
[((0.5738407579726988+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.49205013923731683+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0358805922472856

ITERATION NUMBER :  9

 unitary
[[ 0.58624621 -0.37494968 -0.71814213]
 [ 0.35546006  0.91561477 -0.18787694]
 [ 0.72798594 -0.1451287   0.67005532]]
Current Energy:  -0.5075001020421238
true energy  -0.617824075242885
dvec
[((0.5885032430567038+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.5055208612130737+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03788583901700718

ITERATION NUMBER :  10

 unitary
[[ 0.61469876 -0.36104318 -0.70127973]
 [ 0.34363975  0.92285878 -0.17390627]
 [ 0.70996983 -0.13408762  0.69134894]]
Current Energy:  -0.5423613122556132
true energy  -0.6438687858316976
dvec
[((0.5927657249984052+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.5074191441864391+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.040162064714439706

ITERATION NUMBER :  11

 unitary
[[ 0.64350067 -0.34345837 -0.68406376]
 [ 0.32908231  0.93100991 -0.15787777]
 [ 0.69109458 -0.12351883  0.71213158]]
Current Energy:  -0.5782647355929101
true energy  -0.6704273814069693
dvec
[((0.5855380174916777+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.49618855914199855+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.04207977471378677

ITERATION NUMBER :  12

 unitary
[[ 0.67249379 -0.32203293 -0.66636844]
 [ 0.31151017  0.93989604 -0.13984579]
 [ 0.67135201 -0.11353512  0.73239078]]
Current Energy:  -0.6174003625177467
true energy  -0.6964189875288128
dvec
[((0.563426934668882+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.46822113105160956+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0396157388042844

ITERATION NUMBER :  13

 unitary
[[ 0.70104527 -0.29690847 -0.64836786]
 [ 0.29090404  0.94918153 -0.12012184]
 [ 0.65108399 -0.10440199  0.7517911 ]]
Current Energy:  -0.6535103955388809
true energy  -0.7186838351296667
dvec
[((0.5296243779240519+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.4265959531399517+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.037643563014610346

ITERATION NUMBER :  14

 unitary
[[ 0.72606109 -0.270908   -0.63201594]
 [ 0.26949465  0.95769989 -0.10091357]
 [ 0.63261989 -0.0970555   0.76835689]]
Current Energy:  -0.6866781741247044
true energy  -0.7375455908083248
dvec
[((0.48483431922767395+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.37367237627907823+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03607178118580112

ITERATION NUMBER :  15

 unitary
[[ 0.74801106 -0.24419824 -0.61712776]
 [ 0.24738095  0.9654274  -0.0821742 ]
 [ 0.61585884 -0.09119844  0.78256037]]
Current Energy:  -0.7130133846307043
true energy  -0.7533535056441298
dvec
[((0.43825385747228235+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.3190649921435774+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.034811908706417226

ITERATION NUMBER :  16

 unitary
[[ 0.76727684 -0.21690909 -0.60352025]
 [ 0.22464917  0.97234476 -0.06386249]
 [ 0.60068211 -0.08658011  0.79478607]]
Current Energy:  -0.7363374088452385
true energy  -0.7664456012298699
dvec
[((0.38622754263539516+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.25902758902149203+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03364839854565269

ITERATION NUMBER :  17

 unitary
[[ 0.78415588 -0.18914189 -0.59103714]
 [ 0.20137533  0.97843614 -0.04594232]
 [ 0.58698172 -0.08299436  0.80533496]]
Current Energy:  -0.7558909319009233
true energy  -0.7770930499168273
dvec
[((0.3316955685959261+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.19803360902309403+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.032607922757020236

ITERATION NUMBER :  18

 unitary
[[ 0.79869521 -0.16095176 -0.57981074]
 [ 0.17763032  0.98368804 -0.02837795]
 [ 0.57492037 -0.08032664  0.81425684]]
Current Energy:  -0.7702917327989085
true energy  -0.7855985767956337
dvec
[((0.2820724478763945+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.14865170521576426+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03188897738112548

ITERATION NUMBER :  19

 unitary
[[ 0.81094361 -0.13238702 -0.56995099]
 [ 0.1534836   0.98808851 -0.01112999]
 [ 0.5646355  -0.07845233  0.8216033 ]]
Current Energy:  -0.7820002295139842
true energy  -0.7922993983735024
dvec
[((0.2323654773039386+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.09866530353713886+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03113311657546841

ITERATION NUMBER :  20

 unitary
[[ 0.82123905 -0.10353019 -0.56111311]
 [ 0.12899878  0.99162757  0.00583741]
 [ 0.55581088 -0.07717682  0.82771855]]
Current Energy:  -0.7914352655190193
true energy  -0.7974174176900154
dvec
[((0.181613685292739+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.05544119879954086+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03054910377252529

ITERATION NUMBER :  21

 unitary
[[ 0.82933564 -0.07437823 -0.55377818]
 [ 0.10423816  0.99429641  0.02256213]
 [ 0.54894153 -0.0764364   0.8323585 ]]
Current Energy:  -0.7978420021374353
true energy  -0.8012012715156658
dvec
[((0.13640557134564987+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.02440439416405818+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.030180247766635318

ITERATION NUMBER :  22

 unitary
[[ 0.83524009 -0.04495995 -0.54804434]
 [ 0.07926759  0.99608662  0.03909075]
 [ 0.54414211 -0.07609231  0.83553535]]
Current Energy:  -0.8022703161466986
true energy  -0.8038461439469788
dvec
[((0.09292420629237079+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.0014449454219668811+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03000131446870328

ITERATION NUMBER :  23

 unitary
[[ 0.83902928 -0.01531198 -0.54387077]
 [ 0.05415164  0.99699077  0.05547072]
 [ 0.54138478 -0.07599305  0.83733361]]
Current Energy:  -0.8050948539961322
true energy  -0.8054838851763818
dvec
[((0.04812141440189812+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.011785770347261748+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.03031804429642414

ITERATION NUMBER :  24

 unitary
[[ 0.84042803  0.0145788  -0.54172704]
 [ 0.02895789  0.99700177  0.07175591]
 [ 0.54114893 -0.07599295  0.83748606]]
Current Energy:  -0.8061648917393145
true energy  -0.8061790214585511
dvec
[((0.0069268234457256035+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.008231993295280599+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0076302308745503375

ITERATION NUMBER :  25

 unitary
[[ 0.83854058  0.04481037 -0.54299331]
 [ 0.00377143  0.99611086  0.08802796]
 [ 0.5448261  -0.07586288  0.83511038]]
Current Energy:  -0.806200099332444
true energy  -0.8062031528434435
dvec
[((-0.0010678384010858856+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.00402746193888158+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0023752590880383724

ITERATION NUMBER :  26

 unitary
[[ 0.83612333  0.0513303  -0.54613457]
 [-0.00142759  0.99581239  0.09140917]
 [ 0.54853964 -0.07564969  0.83269526]]
Current Energy:  -0.8061993736950523
true energy  -0.806201909726512
dvec
[((-0.0012746365889709165+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.004253379113857231+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0025610959402677207

ITERATION NUMBER :  27

 unitary
[[ 8.37317743e-01  5.02244786e-02 -5.44404719e-01]
 [-6.37076090e-04  9.95860367e-01  9.08940184e-02]
 [ 5.46716188e-01 -7.57603472e-02  8.33883552e-01]]
Current Energy:  -0.8062046174660273
true energy  -0.8062046055809082
dvec
[((-0.00044266733032686655+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.001021991892239213+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.000677849711724169

ITERATION NUMBER :  28

 unitary
[[ 8.36061241e-01  4.92652635e-02 -5.46419742e-01]
 [ 3.14741233e-04  9.95916957e-01  9.02735579e-02]
 [ 5.48636038e-01 -7.56462037e-02  8.32632062e-01]]
Current Energy:  -0.8062031487586052
true energy  -0.80620465327878
dvec
[((-0.001259924653865746+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.002050650865607573+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.001573896095718731

ITERATION NUMBER :  29

 unitary
[[ 8.36363982e-01  4.88321909e-02 -5.45995153e-01]
 [ 6.43272325e-04  9.95936227e-01  9.00589640e-02]
 [ 5.48174129e-01 -7.56732973e-02  8.32933777e-01]]
Current Energy:  -0.8061941879466855
true energy  -0.8062018894679404
dvec
[((0.004250193069882861+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.0031633682146149505+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.004155813997719719

ITERATION NUMBER :  30

 unitary
[[ 0.83697004  0.04763575 -0.54517152]
 [ 0.00157785  0.99599011  0.08944954]
 [ 0.54724644 -0.07572678  0.83353871]]
Current Energy:  -0.8062029534497432
true energy  -0.8062025927635861
dvec
[((-0.0019186482775878778+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((0.0012216281069513668+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0018260821522259117

ITERATION NUMBER :  31

 unitary
[[ 0.83603453  0.05153837 -0.54625092]
 [-0.00159163  0.99580222  0.09151722]
 [ 0.54867453 -0.07564212  0.83260707]]
Current Energy:  -0.8062023539223688
true energy  -0.8062049620214065
dvec
[((-8.798660667741163e-05+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.0031208107029280507+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.0016860835656905444

ITERATION NUMBER :  32

 unitary
[[ 8.36398151e-01  4.97929761e-02 -5.45856018e-01]
 [-1.67967789e-04  9.95888481e-01  9.05875575e-02]
 [ 5.48122345e-01 -7.56755794e-02  8.32967648e-01]]
Current Energy:  -0.8062048227941037
true energy  -0.806202488766409
dvec
[((1.4273843681374192e-05+0j), -1.0 [0^ 2] +
-1.0 [1^ 3] +
1.0 [2^ 0] +
1.0 [3^ 1]), ((-0.0007783046003266292+0j), -1.0 [0^ 4] +
-1.0 [1^ 5] +
1.0 [4^ 0] +
1.0 [5^ 1])]
New fr values norm
0.00042058441936465563
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.


In [5]:
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()



In [ ]: