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.9240604849722267
Raw energy  -2.9226130106721144 +-  0.0015546808205162462
Raw fidelity witness  0.9980964330753503 +-  0.0020968847718834836
purified energy  -2.9240547872145024 +-  6.265196399726944e-06
Purified fidelity witness  0.9999791040934385 +-  8.944047759823998e-06
Purified fidelity  0.999989552840078 +-  4.471982232122032e-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.826500068188105
true energy  -2.8262712927599694
dvec
[(np.complex128(0.1308286960834566+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.15025170153257272+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.10762797601805571+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(0.334077311773508+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.14129047443858772+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.26172109891078954+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.20326856026467935+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.21923701020511518+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.23626753346275467+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.05189436343149142

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.856339655623998
true energy  -2.8555558613208403
dvec
[(np.complex128(0.12758151992920685+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.1344792126054017+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.08815842395788673+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(0.2495132881654808+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.12452436370256004+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.19543438167750113+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.17709716067107759+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.18366847301629352+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.18987584436738683+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.05658632834541331

ITERATION NUMBER :  2

 unitary
[[ 0.98876569  0.01622336 -0.02189017 -0.08797267 -0.08139094 -0.08506681]
 [ 0.02243121  0.91786943  0.00742315  0.35710982 -0.08703641  0.14783332]
 [-0.03352665 -0.00679841  0.87239824 -0.08278385 -0.47448815 -0.0758873 ]
 [ 0.08675647 -0.35784688  0.08288501  0.92541335 -0.00467283 -0.03372387]
 [ 0.0778939   0.08549264  0.47486769 -0.0131474   0.87206552 -0.02128632]
 [ 0.08438646 -0.14779349  0.07757435 -0.03629735 -0.01187735  0.98161075]]
Current Energy:  -2.880633341830759
true energy  -2.8807008071369946
dvec
[(np.complex128(0.12020933833110042+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.11268727750537971+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.07045251352046437+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(0.16456486687610355+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.113745700678296+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.1282018094873085+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.13909341072337333+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.1532747437562602+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.14027730058926516+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06642486447704787

ITERATION NUMBER :  3

 unitary
[[ 9.92924471e-01  1.13685329e-02 -5.82788321e-03 -7.71008458e-02
  -5.51245432e-02 -7.03884349e-02]
 [ 2.60120172e-02  9.10753389e-01  1.72022964e-02  3.70525894e-01
  -7.31697831e-02  1.64050194e-01]
 [-2.94179400e-02 -1.39734153e-02  8.84464515e-01 -7.00215880e-02
  -4.56743883e-01 -5.60700643e-02]
 [ 7.40865457e-02 -3.72023345e-01  6.97012561e-02  9.21990189e-01
   4.42845077e-03 -3.41471724e-02]
 [ 4.84170784e-02  7.07520005e-02  4.57221534e-01 -1.49016685e-02
   8.84857434e-01 -2.00915937e-02]
 [ 6.87453653e-02 -1.63696520e-01  5.90156046e-02 -3.96866502e-02
   4.51477604e-04  9.81539326e-01]]
Current Energy:  -2.9016685008570664
true energy  -2.9018171584026415
dvec
[(np.complex128(0.09700343479487078+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.08140478841715898+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.05578605742691787+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(0.07732594658989493+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.09163216910784686+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.07162200188319348+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.10289313307193015+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.10757528370821147+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.08289788622762287+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.072374277507389

ITERATION NUMBER :  4

 unitary
[[ 0.99630383  0.00256477  0.00970957 -0.06096579 -0.028077   -0.05265618]
 [ 0.02943233  0.90041034  0.02973046  0.38792353 -0.05255299  0.18510695]
 [-0.02612068 -0.02404032  0.89580628 -0.05416474 -0.43840469 -0.03373991]
 [ 0.05512225 -0.39028103  0.0525056   0.91672752  0.01488953 -0.03569832]
 [ 0.01849966  0.05032573  0.43864786 -0.0202027   0.89657731 -0.02130973]
 [ 0.04949162 -0.18391507  0.03720406 -0.04546634  0.01336448  0.97984483]]
Current Energy:  -2.9158688255617697
true energy  -2.915834151788428
dvec
[(np.complex128(0.07119658460336402+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.04553766988142652+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.03773035301139334+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(-0.0018682072614820931+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.0570462166706141+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.01917671018027106+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.05677861571918899+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.05483183177834733+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.04543135590250334+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.0633949153331249

ITERATION NUMBER :  5

 unitary
[[ 0.99840114 -0.01112853  0.02092841 -0.03979629 -0.00701967 -0.03162738]
 [ 0.03166498  0.88673459  0.04430894  0.40802702 -0.0245098   0.20892354]
 [-0.02410122 -0.0375267   0.90511701 -0.0335062  -0.42128609 -0.01301713]
 [ 0.03000947 -0.41110055  0.02930152  0.90940268  0.02672866 -0.03885271]
 [-0.00421174  0.02415669  0.42102311 -0.03023551  0.90584502 -0.02586261]
 [ 0.02633073 -0.20635521  0.01558458 -0.05366673  0.02446248  0.97621863]]
Current Energy:  -2.9220631852709986
true energy  -2.9221117197026167
dvec
[(np.complex128(0.022471126010961878+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.015875917821374347+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.028992620952896467+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(-0.011676482494269365+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.03643815325152977+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.01133082823483686+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.026644373701059716+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.010333043491147484+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.006314609005834637+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.03234203561467014

ITERATION NUMBER :  6

 unitary
[[ 0.99917466 -0.02734514  0.02119116 -0.01493548 -0.00627409 -0.01381079]
 [ 0.03222009  0.87364114  0.05696848  0.42513296  0.0032665   0.22741792]
 [-0.02341446 -0.05236238  0.90872913 -0.0114376  -0.41324723  0.00415007]
 [ 0.00132114 -0.42820026  0.0041594   0.90186396  0.03793817 -0.04275028]
 [-0.00415835 -0.00122298  0.41290479 -0.04225336  0.9092126  -0.03221725]
 [ 0.00667716 -0.22339342 -0.00303821 -0.06135251  0.03270839  0.97221787]]
Current Energy:  -2.9234823962791356
true energy  -2.9235360911341886
dvec
[(np.complex128(0.018618385445426093+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.005602209115888546+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.015801636371190364+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(-0.0028821006849663287+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.012655323390582476+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.0040517244001687935+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.007084291625292879+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.0019270262661379434+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.016750414049382142+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.016878995689089855

ITERATION NUMBER :  7

 unitary
[[ 9.99175471e-01 -3.32368529e-02  1.90297825e-02 -7.13366425e-03
  -1.01872928e-02 -5.18518287e-03]
 [ 3.21697023e-02  8.69321648e-01  6.46168616e-02  4.30819406e-01
   2.08015275e-02  2.30285864e-01]
 [-2.34790898e-02 -6.16648116e-02  9.10384101e-01  6.32547360e-03
  -4.08380731e-01  5.66910053e-03]
 [-7.47676538e-03 -4.33362326e-01 -1.47933730e-02  8.98781985e-01
   4.61924181e-02 -4.44921616e-02]
 [ 3.85036155e-04 -1.74926832e-02  4.07934203e-01 -5.03338101e-02
   9.10720432e-01 -3.65834014e-02]
 [-2.48385853e-03 -2.26414962e-01 -5.84453768e-03 -6.29379804e-02
   3.38119652e-02  9.71386380e-01]]
Current Energy:  -2.923867255298256
true energy  -2.9238982638863433
dvec
[(np.complex128(0.0033829382037255016+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.0006939208943288477+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.005332134071387876+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(-0.006622665076126175+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.014306209660505689+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(-0.000400496468782513+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.005540674332474944+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.0014158135371026281+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.008267332866617853+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.009689069574836033

ITERATION NUMBER :  8

 unitary
[[ 9.99082326e-01 -3.70065357e-02  1.83516790e-02 -5.04938259e-04
  -1.08741368e-02 -3.12019187e-03]
 [ 3.21432394e-02  8.67859890e-01  6.72102374e-02  4.32838954e-01
   2.68959226e-02  2.30642269e-01]
 [-2.36251594e-02 -6.80388441e-02  9.10729559e-01  1.56870484e-02
  -4.06199224e-01  1.18467459e-02]
 [-1.47762013e-02 -4.34710946e-01 -2.51246469e-02  8.97533799e-01
   5.06870750e-02 -4.51742167e-02]
 [ 1.45602701e-03 -2.30989218e-02  4.06123686e-01 -5.31707113e-02
   9.11179890e-01 -3.81045151e-02]
 [-4.76582467e-03 -2.26524339e-01 -1.22464082e-02 -6.33250109e-02
   3.66412411e-02  9.71164970e-01]]
Current Energy:  -2.92402171971339
true energy  -2.924014161664408
dvec
[(np.complex128(-0.0017221698799865057+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(0.002156464523275947+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.007378160404569581+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(0.0011210521382044938+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.0030686784831837097+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(0.0029323325934867417+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.0031282592108458043+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.0013396881082505668+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(-0.002053675075026527+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.005836262166387031

ITERATION NUMBER :  9

 unitary
[[ 9.99042710e-01 -3.79515465e-02  1.71291492e-02  7.51676657e-04
  -1.33362116e-02 -1.23122197e-03]
 [ 3.21553914e-02  8.67397460e-01  7.01875125e-02  4.32698793e-01
   3.36515942e-02  2.30869476e-01]
 [-2.37022765e-02 -7.04241827e-02  9.10520755e-01  1.88147313e-02
  -4.06019246e-01  1.49989934e-02]
 [-1.60574965e-02 -4.34691030e-01 -2.85566461e-02  8.97325651e-01
   5.22030906e-02 -4.52978584e-02]
 [ 4.06962464e-03 -2.96147642e-02  4.05806394e-01 -5.63591360e-02
   9.10860229e-01 -3.98103697e-02]
 [-6.59476679e-03 -2.26690174e-01 -1.54255932e-02 -6.36201140e-02
   3.80337817e-02  9.70997445e-01]]
Current Energy:  -2.924033944787937
true energy  -2.9240358762201213
dvec
[(np.complex128(8.198659875297584e-06+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), (np.complex128(-0.0010387862979338303+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), (np.complex128(0.0020198968073916425+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), (np.complex128(-0.00438860516722391+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), (np.complex128(0.0035205525836188474+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), (np.complex128(-0.003132800927515167+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), (np.complex128(0.0014793613368340808+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), (np.complex128(0.0015011644487954168+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), (np.complex128(0.004488888499656603+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.003813285179298179
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()

png