-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathestimator.py
95 lines (83 loc) · 3.44 KB
/
estimator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# (C) Copyright 2023 Beijing Academy of Quantum Information Sciences
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Pre-build wrapper to calculate expectation value"""
from typing import List, Optional
from ..circuits.quantum_circuit import QuantumCircuit
from ..simulators import simulate
from ..tasks.tasks import Task
from .hamiltonian import Hamiltonian
def execute_circuit(circ: QuantumCircuit, observables: Hamiltonian):
"""Execute circuit on quafu simulator"""
sim_res = simulate(circ, hamiltonian=observables)
expectations = sim_res["pauli_expects"]
return sum(expectations)
class Estimator:
"""Estimate expectation for quantum circuits and observables"""
def __init__(
self,
circ: QuantumCircuit,
backend: str = "sim",
task: Optional[Task] = None,
**task_options
) -> None:
"""
Args:
circ: quantum circuit.
backend: run on simulator (sim) or real machines (ScQ-PXX)
task: task instance for real machine execution (should be none if backend is "sim")
task_options: options to config a task instance
"""
self._circ = circ
self._circ.get_parameter_grads() # parameter shift currently requires calling this for initialization
self._backend = backend
self._task = None
if backend != "sim":
if task is not None:
self._task = task
else:
self._task = Task()
self._task.config(backend=self._backend)
self._task.config(**task_options)
def _run_real_machine(self, observables: Hamiltonian):
"""Submit to quafu service"""
if not isinstance(self._task, Task):
raise ValueError("task not set")
# TODO(zhaoyilun): replace old `submit` API in the future,
# investigate the best implementation for calculating
# expectation on real devices.
obs = observables.to_legacy_quafu_pauli_list()
_, obsexp = self._task.submit(self._circ, obs)
return sum(obsexp)
def _run_simulation(self, observables: Hamiltonian):
"""Run using quafu simulator"""
# sim_state = simulate(self._circ).get_statevector()
# expectation = np.matmul(
# np.matmul(sim_state.conj().T, observables.get_matrix()), sim_state
# ).real
# return expectation
return execute_circuit(self._circ, observables)
def run(self, observables: Hamiltonian, params: List[float]):
"""Calculate estimation for given observables
Args:
observables: observables to be estimated.
paras_list: list of parameters of self.circ.
Returns:
Expectation value
"""
if params is not None:
self._circ._update_params(params)
if self._backend == "sim":
return self._run_simulation(observables)
else:
return self._run_real_machine(observables)