VQE Example
[1]:
# Where to find this code?
# https://learning.quantum.ibm.com/tutorial/variational-quantum-eigensolver
[1]:
# General imports
import numpy as np
# Pre-defined ansatz circuit and operator class for Hamiltonian
from qiskit.circuit.library import EfficientSU2
from qiskit.quantum_info import SparsePauliOp
# SciPy minimizer routine
from scipy.optimize import minimize
# Plotting functions
import matplotlib.pyplot as plt
[2]:
# runtime imports
from qiskit_ibm_runtime import QiskitRuntimeService, Session
#from qiskit_ibm_runtime import EstimatorV2 as Estimator
# To run on hardware, select the backend with the fewest number of jobs in the queue
#service = QiskitRuntimeService(channel="ibm_quantum")
#backend = service.least_busy(operational=True, simulator=False)
import QuantumRingsLib
from QuantumRingsLib import QuantumRingsProvider
from quantumrings.toolkit.qiskit import QrBackendV2
from quantumrings.toolkit.qiskit import QrEstimatorV2 as Estimator
provider = QuantumRingsProvider()
backend = QrBackendV2(provider, num_qubits = 2)
[3]:
hamiltonian = SparsePauliOp.from_list(
[("YZ", 0.3980), ("ZI", -0.3980), ("ZZ", -0.0113), ("XX", 0.1810)]
)
[4]:
ansatz = EfficientSU2(hamiltonian.num_qubits)
ansatz.decompose().draw("mpl", style="iqp")
[4]:

[5]:
num_params = ansatz.num_parameters
num_params
[5]:
16
[6]:
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)
ansatz_isa = pm.run(ansatz)
[7]:
ansatz_isa.draw(output="mpl", idle_wires=False, style="iqp")
[7]:

[8]:
hamiltonian_isa = hamiltonian.apply_layout(layout=ansatz_isa.layout)
[9]:
def cost_func(params, ansatz, hamiltonian, estimator):
"""Return estimate of energy from estimator
Parameters:
params (ndarray): Array of ansatz parameters
ansatz (QuantumCircuit): Parameterized ansatz circuit
hamiltonian (SparsePauliOp): Operator representation of Hamiltonian
estimator (EstimatorV2): Estimator primitive instance
cost_history_dict: Dictionary for storing intermediate results
Returns:
float: Energy estimate
"""
pub = (ansatz, [hamiltonian], [params])
result = estimator.run(pubs=[pub]).result()
energy = result[0].data.evs[0]
cost_history_dict["iters"] += 1
cost_history_dict["prev_vector"] = params
cost_history_dict["cost_history"].append(energy)
print(f"Iters. done: {cost_history_dict['iters']} [Current cost: {energy}]")
return energy
[10]:
cost_history_dict = {
"prev_vector": None,
"iters": 0,
"cost_history": [],
}
[11]:
x0 = 2 * np.pi * np.random.random(num_params)
[12]:
x0
[12]:
array([4.56540515, 1.75128513, 5.91131155, 1.7391921 , 2.59282932,
2.7492441 , 5.15476581, 0.99109984, 3.39292909, 2.89356259,
4.0750673 , 1.23683026, 4.18918897, 0.82554628, 4.96674947,
0.39142382])
[13]:
with Session(backend=backend) as session:
#estimator = Estimator(mode=session)
estimator = Estimator(backend=backend)
estimator.options.default_shots = 10000
res = minimize(
cost_func,
x0,
args=(ansatz_isa, hamiltonian_isa, estimator),
method="cobyla",
)
C:\Users\vkasi\.conda\envs\QiskitNew\Lib\site-packages\qiskit_ibm_runtime\session.py:157: UserWarning: Session is not supported in local testing mode or when using a simulator.
warnings.warn(
Iters. done: 1 [Current cost: [0.015363085937500007]]
Iters. done: 2 [Current cost: [0.15521210937500002]]
Iters. done: 3 [Current cost: [0.037598632812500005]]
Iters. done: 4 [Current cost: [-0.16597773437500002]]
Iters. done: 5 [Current cost: [-0.5212265625]]
Iters. done: 6 [Current cost: [-0.7275572265625]]
Iters. done: 7 [Current cost: [-0.35972734375]]
Iters. done: 8 [Current cost: [-0.6028781250000002]]
Iters. done: 9 [Current cost: [-0.49941503906250007]]
Iters. done: 10 [Current cost: [-0.5935625]]
Iters. done: 11 [Current cost: [-0.3924388671875]]
Iters. done: 12 [Current cost: [-0.58218671875]]
Iters. done: 13 [Current cost: [-0.35479648437500005]]
Iters. done: 14 [Current cost: [-0.53388359375]]
Iters. done: 15 [Current cost: [-0.47557011718750003]]
Iters. done: 16 [Current cost: [-0.718384375]]
Iters. done: 17 [Current cost: [-0.599245703125]]
Iters. done: 18 [Current cost: [-0.7352267578125]]
Iters. done: 19 [Current cost: [-0.6604880859375002]]
Iters. done: 20 [Current cost: [-0.7102667968750002]]
Iters. done: 21 [Current cost: [-0.6553755859375001]]
Iters. done: 22 [Current cost: [-0.7395595703125001]]
Iters. done: 23 [Current cost: [-0.7673978515625]]
Iters. done: 24 [Current cost: [-0.7679607421875]]
Iters. done: 25 [Current cost: [-0.7433740234375]]
Iters. done: 26 [Current cost: [-0.7296439453125]]
Iters. done: 27 [Current cost: [-0.5866796875]]
Iters. done: 28 [Current cost: [-0.7602619140625]]
Iters. done: 29 [Current cost: [-0.79378984375]]
Iters. done: 30 [Current cost: [-0.7927158203125]]
Iters. done: 31 [Current cost: [-0.739683203125]]
Iters. done: 32 [Current cost: [-0.797621875]]
Iters. done: 33 [Current cost: [-0.7980318359375]]
Iters. done: 34 [Current cost: [-0.7930044921875]]
Iters. done: 35 [Current cost: [-0.6778244140625]]
Iters. done: 36 [Current cost: [-0.7679976562499999]]
Iters. done: 37 [Current cost: [-0.7977531250000001]]
Iters. done: 38 [Current cost: [-0.7558951171875001]]
Iters. done: 39 [Current cost: [-0.74812578125]]
Iters. done: 40 [Current cost: [-0.7780849609374999]]
Iters. done: 41 [Current cost: [-0.7604664062500001]]
Iters. done: 42 [Current cost: [-0.7809279296875]]
Iters. done: 43 [Current cost: [-0.7705970703125]]
Iters. done: 44 [Current cost: [-0.7465333984375]]
Iters. done: 45 [Current cost: [-0.80120078125]]
Iters. done: 46 [Current cost: [-0.8165593750000001]]
Iters. done: 47 [Current cost: [-0.8094785156250001]]
Iters. done: 48 [Current cost: [-0.8006482421875001]]
Iters. done: 49 [Current cost: [-0.79741328125]]
Iters. done: 50 [Current cost: [-0.8088933593750001]]
Iters. done: 51 [Current cost: [-0.7971013671875001]]
Iters. done: 52 [Current cost: [-0.8151621093750001]]
Iters. done: 53 [Current cost: [-0.7965101562500001]]
Iters. done: 54 [Current cost: [-0.784671875]]
Iters. done: 55 [Current cost: [-0.7975800781250001]]
Iters. done: 56 [Current cost: [-0.8030785156250001]]
Iters. done: 57 [Current cost: [-0.8019964843750002]]
Iters. done: 58 [Current cost: [-0.785401171875]]
Iters. done: 59 [Current cost: [-0.8018156250000001]]
Iters. done: 60 [Current cost: [-0.804292578125]]
Iters. done: 61 [Current cost: [-0.8035927734375]]
Iters. done: 62 [Current cost: [-0.8014998046875]]
Iters. done: 63 [Current cost: [-0.8056765625000001]]
Iters. done: 64 [Current cost: [-0.8035894531250001]]
Iters. done: 65 [Current cost: [-0.8091031249999999]]
Iters. done: 66 [Current cost: [-0.805011328125]]
Iters. done: 67 [Current cost: [-0.8108826171875001]]
Iters. done: 68 [Current cost: [-0.8020224609375001]]
Iters. done: 69 [Current cost: [-0.808287109375]]
Iters. done: 70 [Current cost: [-0.81293125]]
Iters. done: 71 [Current cost: [-0.812403125]]
Iters. done: 72 [Current cost: [-0.8075662109375]]
Iters. done: 73 [Current cost: [-0.8250837890625001]]
Iters. done: 74 [Current cost: [-0.8006546875]]
Iters. done: 75 [Current cost: [-0.8078375]]
Iters. done: 76 [Current cost: [-0.8121939453125]]
Iters. done: 77 [Current cost: [-0.80466171875]]
Iters. done: 78 [Current cost: [-0.8011666015625001]]
Iters. done: 79 [Current cost: [-0.8121320312500001]]
Iters. done: 80 [Current cost: [-0.7989496093750001]]
Iters. done: 81 [Current cost: [-0.807495703125]]
Iters. done: 82 [Current cost: [-0.8061029296875001]]
Iters. done: 83 [Current cost: [-0.809749609375]]
Iters. done: 84 [Current cost: [-0.8035816406249999]]
Iters. done: 85 [Current cost: [-0.805676171875]]
Iters. done: 86 [Current cost: [-0.815396484375]]
Iters. done: 87 [Current cost: [-0.7998855468749999]]
Iters. done: 88 [Current cost: [-0.816166796875]]
Iters. done: 89 [Current cost: [-0.806573828125]]
Iters. done: 90 [Current cost: [-0.8165556640625]]
Iters. done: 91 [Current cost: [-0.7993876953125]]
Iters. done: 92 [Current cost: [-0.800240234375]]
Iters. done: 93 [Current cost: [-0.805790625]]
Iters. done: 94 [Current cost: [-0.803139453125]]
Iters. done: 95 [Current cost: [-0.8075466796875002]]
Iters. done: 96 [Current cost: [-0.8124552734374999]]
Iters. done: 97 [Current cost: [-0.8018402343750001]]
Iters. done: 98 [Current cost: [-0.8073445312499999]]
Iters. done: 99 [Current cost: [-0.8086408203125]]
Iters. done: 100 [Current cost: [-0.7999519531250001]]
Iters. done: 101 [Current cost: [-0.809661328125]]
Iters. done: 102 [Current cost: [-0.7982169921875]]
Iters. done: 103 [Current cost: [-0.8020066406249999]]
Iters. done: 104 [Current cost: [-0.8090115234375]]
Iters. done: 105 [Current cost: [-0.8161349609375]]
Iters. done: 106 [Current cost: [-0.794966015625]]
Iters. done: 107 [Current cost: [-0.7994638671875]]
Iters. done: 108 [Current cost: [-0.8048728515625001]]
Iters. done: 109 [Current cost: [-0.8167121093750002]]
Iters. done: 110 [Current cost: [-0.8096531250000001]]
Iters. done: 111 [Current cost: [-0.8037099609374999]]
Iters. done: 112 [Current cost: [-0.8068578125000001]]
Iters. done: 113 [Current cost: [-0.800751953125]]
Iters. done: 114 [Current cost: [-0.7949529296875001]]
Iters. done: 115 [Current cost: [-0.81407109375]]
Iters. done: 116 [Current cost: [-0.8046902343750001]]
Iters. done: 117 [Current cost: [-0.8112083984375001]]
Iters. done: 118 [Current cost: [-0.81982109375]]
Iters. done: 119 [Current cost: [-0.8008373046874999]]
Iters. done: 120 [Current cost: [-0.8044718750000002]]
Iters. done: 121 [Current cost: [-0.8091095703125]]
Iters. done: 122 [Current cost: [-0.8040011718750001]]
Iters. done: 123 [Current cost: [-0.810244140625]]
Iters. done: 124 [Current cost: [-0.8020302734375]]
Iters. done: 125 [Current cost: [-0.80039765625]]
Iters. done: 126 [Current cost: [-0.8167541015625001]]
Iters. done: 127 [Current cost: [-0.7982646484375]]
Iters. done: 128 [Current cost: [-0.7976980468750001]]
Iters. done: 129 [Current cost: [-0.80412578125]]
Iters. done: 130 [Current cost: [-0.7943396484375]]
Iters. done: 131 [Current cost: [-0.7919171875]]
Iters. done: 132 [Current cost: [-0.8077927734375]]
Iters. done: 133 [Current cost: [-0.8179365234374999]]
Iters. done: 134 [Current cost: [-0.8018642578124999]]
Iters. done: 135 [Current cost: [-0.801982421875]]
Iters. done: 136 [Current cost: [-0.810073046875]]
Iters. done: 137 [Current cost: [-0.8089564453125]]
Iters. done: 138 [Current cost: [-0.80851328125]]
Iters. done: 139 [Current cost: [-0.8071970703125]]
Iters. done: 140 [Current cost: [-0.817515234375]]
Iters. done: 141 [Current cost: [-0.8143800781250001]]
Iters. done: 142 [Current cost: [-0.8080625]]
Iters. done: 143 [Current cost: [-0.8057224609375]]
Iters. done: 144 [Current cost: [-0.8136078125]]
Iters. done: 145 [Current cost: [-0.8082853515625]]
Iters. done: 146 [Current cost: [-0.805993359375]]
Iters. done: 147 [Current cost: [-0.80845078125]]
Iters. done: 148 [Current cost: [-0.8013568359375001]]
Iters. done: 149 [Current cost: [-0.7977021484375]]
[15]:
res
[15]:
message: Optimization terminated successfully.
success: True
status: 1
fun: -0.8160978515624999
x: [ 4.822e+00 1.912e+00 ... 1.352e+00 6.207e+00]
nfev: 165
maxcv: 0.0
[16]:
all(cost_history_dict["prev_vector"] == res.x)
[16]:
True
[17]:
cost_history_dict["iters"] == res.nfev
[17]:
True
[18]:
fig, ax = plt.subplots()
ax.plot(range(cost_history_dict["iters"]), cost_history_dict["cost_history"])
ax.set_xlabel("Iterations")
ax.set_ylabel("Cost")
plt.draw()

[ ]:
[ ]: