-
Notifications
You must be signed in to change notification settings - Fork 2
/
VQE.py
81 lines (68 loc) · 2.6 KB
/
VQE.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from qiskit.circuit.library import RealAmplitudes
from qiskit.algorithms.optimizers import COBYLA
from qiskit.algorithms.minimum_eigensolvers import SamplingVQE
from qiskit.primitives import Sampler
from qiskit.opflow import PauliSumOp
def get_min(qubit_op):
optimizer = COBYLA(maxiter=50)
ansatz = RealAmplitudes(reps=1)
counts = []
values = []
def store_intermediate_result(eval_count, parameters, mean, std):
counts.append(eval_count)
values.append(mean)
# initialize VQE using CVaR with alpha = 0.1 or 0.05 which can be set by user,
# this method set alpha = 0.1
vqe = SamplingVQE(
Sampler(),
ansatz=ansatz,
optimizer=optimizer,
aggregation=0.1,
callback=store_intermediate_result,
)
raw_result = vqe.compute_minimum_eigenvalue(qubit_op)
# We only need the final qubit and the lowest value
return raw_result.best_measurement['bitstring'], raw_result.best_measurement['value']
def test(qubit_op):
"""
This does not work because the qiskit-terra requires the version of qiskit less than 1.0.0,
but other packages require qiskit more than 1.0.0.
:param qubit_op:
:return:
"""
from qiskit.utils import algorithm_globals
from qiskit_aer.primitives import Estimator as AerEstimator
from qiskit_ibm_runtime import Options
seed = 170
algorithm_globals.random_seed = seed
noiseless_estimator = AerEstimator(
run_options={"seed": seed, "shots": 1024},
transpile_options={"seed_transpiler": seed},
)
# set classical optimizer
optimizer = COBYLA(maxiter=50)
# set variational ansatz
ansatz = RealAmplitudes(reps=1)
# ansatz = EfficientSU2(qubit_op.num_qubits, entanglement='linear')
counts = []
values = []
backend = "ibmq_qasm_simulator" # use the simulator
options = Options()
options.simulator.seed_simulator = 42
options.execution.shots = 1000
options.optimization_level = 0 # no optimization
options.resilience_level = 0 # no error mitigation
def store_intermediate_result(eval_count, parameters, mean, std):
counts.append(eval_count)
values.append(mean)
# initialize VQE using CVaR with alpha = 0.1
vqe = SamplingVQE(
Sampler(backend=backend, Options=options),
ansatz=ansatz,
optimizer=optimizer,
aggregation=0.1,
callback=store_intermediate_result,
)
raw_result = vqe.compute_minimum_eigenvalue(PauliSumOp(qubit_op))
# print(vqe.sampler.circuits[0].depth())
return raw_result.best_measurement['bitstring'], raw_result.best_measurement['value']