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.923310113426198 +-  0.0015387966847919925
Raw fidelity witness  0.9998962380218406 +-  0.0020851195410641725
purified energy  -2.924053293202899 +-  6.782494183238227e-06
Purified fidelity witness  0.9999777468102539 +-  9.408923684089392e-06
Purified fidelity  0.9999888737162103 +-  4.704445649606818e-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.7806001654427446
true energy  -2.78086438438673
dvec
[((0.2911168206360509+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.2925072879800671+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.3276477356656424+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.22723047667465024+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.2358797536275172+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.3286782257732593+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.47016052534132213+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.31157452190474044+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.28897191928863986+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06521539918959801

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.835028807279577
true energy  -2.8350117950387927
dvec
[((0.23365247173156004+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.2300811849902434+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.23957524121500343+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.19743096867270954+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.18843250872462958+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.26358064489649924+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.34335680776514943+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.25136757751079997+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.2241578324498773+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.06671397112242869

ITERATION NUMBER :  2

 unitary
[[ 9.91754397e-01  1.49912987e-04  2.79681237e-03 -7.99460714e-02
  -7.02205932e-02 -7.13657141e-02]
 [ 1.52286835e-03  8.77928747e-01  1.36883859e-02  3.60559141e-01
  -7.62447176e-02 -3.05344515e-01]
 [ 9.60437622e-03  1.37508107e-02  9.37982973e-01 -6.68677122e-02
   3.30255587e-01 -7.97907827e-02]
 [ 8.03227992e-02 -3.58595449e-01  6.76250383e-02  9.25387047e-01
   2.22821361e-02  5.95543972e-02]
 [ 6.96504132e-02  7.75009388e-02 -3.29929403e-01  2.52553246e-02
   9.37888602e-01  4.01892303e-03]
 [ 7.08923163e-02  3.07335185e-01  8.10320382e-02  4.64265347e-02
  -7.45212519e-03  9.44321034e-01]]
Current Energy:  -2.8764859703482992
true energy  -2.8764618025436715
dvec
[((0.17683705746535822+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.16967720016336202+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.15313562820949236+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.1624347501703286+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.14315767509065064+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.19819114480277386+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.22295128108154222+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.18777245009067767+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.14927245326897776+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.07156426974656199

ITERATION NUMBER :  3

 unitary
[[ 9.95814979e-01  1.77168397e-03 -3.25347965e-04 -5.93830522e-02
  -5.32271291e-02 -4.46072629e-02]
 [ 5.35749376e-03  8.77679206e-01  9.52251665e-03  3.81210199e-01
  -5.74948562e-02 -2.84487181e-01]
 [ 1.41341060e-02  1.01287182e-02  9.33816667e-01 -3.78044424e-02
   3.50439818e-01 -5.87096841e-02]
 [ 5.94620987e-02 -3.77328551e-01  3.92710456e-02  9.20634691e-01
   1.47357861e-02  6.89915315e-02]
 [ 5.14180963e-02  5.98214849e-02 -3.50006086e-01  2.02639448e-02
   9.33118116e-01  1.23799508e-02]
 [ 4.40963637e-02  2.89167834e-01  6.20417076e-02  4.17660142e-02
  -1.12510553e-02  9.53266654e-01]]
Current Energy:  -2.9059250981058153
true energy  -2.905895665531206
dvec
[((0.10712028116499778+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.10513303841986035+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.07085875963239022+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.11200883886140417+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.08737314114932304+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.12075922829429009+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.10678803862894143+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.12596682277908816+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.09044822158791423+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.08277819841336062

ITERATION NUMBER :  4

 unitary
[[ 9.98591343e-01  9.86400043e-04 -6.41256944e-03 -3.69353164e-02
  -3.27280746e-02 -1.83818291e-02]
 [ 8.12140016e-03  8.75984796e-01  3.05541484e-03  4.04218236e-01
  -3.62607992e-02 -2.60515100e-01]
 [ 1.71365894e-02  5.18838423e-03  9.27033915e-01 -9.13950489e-03
   3.72586973e-01 -3.71881093e-02]
 [ 3.58653603e-02 -3.98397564e-01  1.12761182e-02  9.13129126e-01
   5.98857558e-03  7.76224322e-02]
 [ 2.87912934e-02  3.95210117e-02 -3.72197627e-01  1.28924555e-02
   9.26549089e-01  2.04608790e-02]
 [ 1.84512237e-02  2.68961365e-01  4.35825608e-02  3.44797472e-02
  -1.62449206e-02  9.61232098e-01]]
Current Energy:  -2.9217148543517415
true energy  -2.921758008688324
dvec
[((0.022270467490081373+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.02870213638285865+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.005212514590905883+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.04950006790751265+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.025221759828651956+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.02920109464386811+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.006656462628961187+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.04651434937892024+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.03462350037627562+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.03932840510746992

ITERATION NUMBER :  5

 unitary
[[ 9.99739762e-01 -3.36920924e-03 -1.71758084e-02 -1.15830027e-02
  -5.68179393e-03  6.89930605e-03]
 [ 9.34448431e-03  8.72221637e-01 -6.65551302e-03  4.32180784e-01
  -1.13384255e-02 -2.28449141e-01]
 [ 1.83003594e-02  1.93772940e-04  9.16858975e-01  1.76339549e-02
   3.98231732e-01 -1.16280184e-02]
 [ 8.14664068e-03 -4.24364978e-01 -1.46732042e-02  9.01260018e-01
  -3.79067850e-03  8.57245937e-02]
 [-1.74488485e-03  1.53021802e-02 -3.97887127e-01  1.92798101e-03
   9.16873017e-01  2.80853342e-02]
 [-5.36415907e-03  2.42689586e-01  2.23796074e-02  2.23899650e-02
  -2.40862288e-02  9.69273278e-01]]
Current Energy:  -2.9236925009781163
true energy  -2.923680902873468
dvec
[((-0.0008831076418034247+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.001417174180218167+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.015429286227810035+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.0024663081276074927+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.007661199505215627+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.008090714558493084+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.013348230219161834+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.002010545482956594+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.013811467195305696+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.014037184113811685

ITERATION NUMBER :  6

 unitary
[[ 9.99517350e-01 -7.55504407e-03 -2.58652027e-02 -3.59224194e-03
   1.37458626e-02  6.09310291e-03]
 [ 9.15214549e-03  8.70717965e-01 -1.17717755e-02  4.44663281e-01
   9.14168456e-06 -2.09529129e-01]
 [ 1.80854748e-02  5.20622664e-03  9.12680862e-01  1.46771373e-02
   4.07968970e-01  2.31430968e-03]
 [-8.73541912e-04 -4.36108894e-01 -1.10871822e-02  8.95448096e-01
  -2.30987970e-03  8.86155500e-02]
 [-2.32618259e-02  4.07840751e-03 -4.07425084e-01 -3.77949400e-03
   9.12404296e-01  3.08410693e-02]
 [-3.51346841e-03  2.27064839e-01  9.37744029e-03  1.43061975e-02
  -2.97562749e-02  9.73268295e-01]]
Current Energy:  -2.9239124746768237
true energy  -2.923923275884299
dvec
[((0.009588389811279796+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.00042682481350120316+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.007988698496559227+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.0012235326722757883+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.009014465452053857+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((7.232024338735266e-05+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((6.320446913860456e-05+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.0017907260065313513+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.006850554560939836+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.008730083325537267

ITERATION NUMBER :  7

 unitary
[[ 9.99547615e-01 -8.44055434e-03 -2.54610175e-02 -3.97084926e-03
   1.29140063e-02  1.58734465e-03]
 [ 9.06379060e-03  8.70847279e-01 -1.33766895e-02  4.44025170e-01
   3.64004219e-03 -2.10219711e-01]
 [ 1.81545531e-02  1.11946831e-02  9.14036122e-01  5.11842720e-03
   4.04981926e-01  6.81902169e-03]
 [-3.17517002e-04 -4.35431648e-01 -1.25848513e-03  8.95855379e-01
   2.07804354e-03  8.85235369e-02]
 [-2.21769952e-02  6.55157362e-04 -4.04589782e-01 -5.50840016e-03
   9.13663159e-01  3.16883243e-02]
 [ 9.51420034e-04  2.27656109e-01  4.03630530e-03  1.45766624e-02
  -3.20145637e-02  9.73097163e-01]]
Current Energy:  -2.924000323250029
true energy  -2.924011580243367
dvec
[((-0.002506486236702607+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.000970222916061636+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.0009972475304001272+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.0007410392613995068+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.006419835105327357+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.0010387340913406706+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.0038948199868477553+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.003030044782189878+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.008797993873664919+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.005407411310355213

ITERATION NUMBER :  8

 unitary
[[ 9.99550786e-01 -1.01115340e-02 -2.52420351e-02 -5.16232450e-04
   1.24427757e-02  1.93233999e-03]
 [ 8.96175359e-03  8.70772598e-01 -1.52575094e-02  4.43801445e-01
   7.90442346e-03 -2.10759993e-01]
 [ 1.81687496e-02  1.42636280e-02  9.14025309e-01  1.69440189e-04
   4.04897214e-01  9.07727969e-03]
 [-4.06453662e-03 -4.35146954e-01  3.92759243e-03  8.95969518e-01
   4.28599430e-03  8.85179911e-02]
 [-2.16911717e-02 -3.39371787e-03 -4.04561828e-01 -7.57097885e-03
   9.13631920e-01  3.26664495e-02]
 [ 8.84691687e-04  2.28221381e-01  1.44324578e-03  1.48756369e-02
  -3.31555119e-02  9.72929372e-01]]
Current Energy:  -2.9240143866114994
true energy  -2.924044410391702
dvec
[((0.00589705645570891+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.00404162819963827+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.006644391930941089+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.00017639805829225072+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.003111598236476687+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.0047583482414612355+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.00012995239687356067+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.00233567632163785+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((-4.8472672176985004e-05+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.006109753576019443

ITERATION NUMBER :  9

 unitary
[[ 9.99545355e-01 -9.97828078e-03 -2.53754603e-02 -1.44023218e-03
   1.27757045e-02  5.56824670e-04]
 [ 8.88953193e-03  8.70820365e-01 -1.65944528e-02  4.44207076e-01
   1.08885603e-02 -2.09473160e-01]
 [ 1.81680785e-02  1.53909500e-02  9.13861755e-01 -4.02985386e-04
   4.05131773e-01  1.25625422e-02]
 [-3.04982425e-03 -4.35551155e-01  4.54429259e-03  8.95762262e-01
   4.57562845e-03  8.86243347e-02]
 [-2.20476040e-02 -6.27665741e-03 -4.04861923e-01 -9.03923469e-03
   9.13436888e-01  3.33567733e-02]
 [ 2.14053604e-03  2.27137048e-01 -1.89103574e-03  1.43561567e-02
  -3.46205637e-02  9.73137154e-01]]
Current Energy:  -2.9240355355406593
true energy  -2.9240425939686174
dvec
[((-0.005190058381876124+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((-0.005883606666771651+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.0019075442067241885+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.005729838893404778+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((-0.0013449985210631638+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.0033416372143696693+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.005230313644305731+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((-0.0005180992693374492+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.002032508436276999+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.005124913817028045

ITERATION NUMBER :  10

 unitary
[[ 9.99533626e-01 -1.10223599e-02 -2.53904255e-02  6.78878622e-04
   1.28598297e-02  7.26520586e-04]
 [ 8.84407915e-03  8.69761815e-01 -1.72292062e-02  4.45908089e-01
   1.23918987e-02 -2.10123152e-01]
 [ 1.81474567e-02  1.73894657e-02  9.13097242e-01 -4.55627222e-03
   4.06758244e-01  1.21932231e-02]
 [-5.35035662e-03 -4.37131362e-01  8.85693586e-03  8.94892170e-01
   6.39807402e-03  8.90851777e-02]
 [-2.21708144e-02 -7.69864194e-03 -4.06484802e-01 -9.74716151e-03
   9.12683189e-01  3.36701819e-02]
 [ 2.19335390e-03  2.27919729e-01 -1.88911427e-03  1.47564160e-02
  -3.46014071e-02  9.72948718e-01]]
Current Energy:  -2.9240348869683186
true energy  -2.9240398745392264
dvec
[((0.004042167816728785+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((3.609654478309299e-05+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((0.002981406788836377+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((-0.006636552029626864+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((-0.0007365370578159158+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((-0.0024062148842306785+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((0.0066385112909700325+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.0009670130370709601+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((0.005726425249959032+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.005200993283179341

ITERATION NUMBER :  11

 unitary
[[ 0.99947936 -0.01053793 -0.02640569 -0.00124032  0.01516341 -0.00111345]
 [ 0.00887189  0.8709544  -0.01697012  0.44337877  0.01174756 -0.21059204]
 [ 0.01817401  0.0170084   0.91362188 -0.00333667  0.40557752  0.01308522]
 [-0.00326344 -0.43471497  0.00753572  0.89615454  0.00591946  0.0884729 ]
 [-0.02460974 -0.00704587 -0.4052724  -0.00944276  0.91317296  0.03354066]
 [ 0.00396503  0.22805999 -0.00270504  0.01484809 -0.03491472  0.97289573]]
Current Energy:  -2.9240347808531197
true energy  -2.9240466574369943
dvec
[((-0.0014939403782711948+0j), -1.0 [0^ 6] +
-1.0 [1^ 7] +
1.0 [6^ 0] +
1.0 [7^ 1]), ((0.0008572335746679094+0j), -1.0 [2^ 6] +
-1.0 [3^ 7] +
1.0 [6^ 2] +
1.0 [7^ 3]), ((-0.005459424058817685+0j), -1.0 [4^ 6] +
-1.0 [5^ 7] +
1.0 [6^ 4] +
1.0 [7^ 5]), ((0.0039320590777360085+0j), -1.0 [0^ 8] +
-1.0 [1^ 9] +
1.0 [8^ 0] +
1.0 [9^ 1]), ((0.0019486339517142386+0j), -1.0 [2^ 8] +
-1.0 [3^ 9] +
1.0 [8^ 2] +
1.0 [9^ 3]), ((0.0010438622826220156+0j), -1.0 [4^ 8] +
-1.0 [5^ 9] +
1.0 [8^ 4] +
1.0 [9^ 5]), ((-0.006087631242539529+0j), -1.0 [0^ 10] +
-1.0 [1^ 11] +
1.0 [10^ 0] +
1.0 [11^ 1]), ((0.0020174858826655373+0j), -1.0 [2^ 10] +
-1.0 [3^ 11] +
1.0 [10^ 2] +
1.0 [11^ 3]), ((-0.0022790059841688486+0j), -1.0 [4^ 10] +
-1.0 [5^ 11] +
1.0 [10^ 4] +
1.0 [11^ 5])]
New fr values norm
0.0049865164006613885
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