Skip to content

Commit d2bc2db

Browse files
committed
ComputeUncompute done
1 parent d7b671f commit d2bc2db

File tree

12 files changed

+359
-181
lines changed

12 files changed

+359
-181
lines changed

qiskit_algorithms/algorithm_job.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,6 @@ class AlgorithmJob(PrimitiveJob):
2020
"""
2121
This class is introduced for typing purposes and provides no
2222
additional function beyond that inherited from its parents.
23-
24-
Update: :meth:`AlgorithmJob.submit()` method added. See its
25-
documentation for more info.
2623
"""
2724

28-
def submit(self) -> None:
29-
"""
30-
Submit the job for execution.
31-
32-
For V1 primitives, Qiskit ``PrimitiveJob`` subclassed JobV1 and defined ``submit()``.
33-
``PrimitiveJob`` was updated for V2 primitives, no longer subclasses ``JobV1``, and
34-
now has a private ``_submit()`` method, with ``submit()`` being deprecated as of
35-
Qiskit version 0.46. This maintains the ``submit()`` for ``AlgorithmJob`` here as
36-
it's called in many places for such a job. An alternative could be to make
37-
0.46 the required minimum version and alter all algorithm's call sites to use
38-
``_submit()`` and make this an empty class again as it once was. For now this
39-
way maintains compatibility with the current min version of 0.44.
40-
"""
41-
# TODO: Considering changing this in the future - see above docstring.
42-
try:
43-
super()._submit()
44-
except AttributeError:
45-
super().submit()
25+
pass

qiskit_algorithms/gradients/base/base_estimator_gradient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def run(
149149
job = AlgorithmJob(
150150
self._run, circuits, observables, parameter_values, parameters, **opts.__dict__
151151
)
152-
job.submit()
152+
job._submit()
153153
return job
154154

155155
@abstractmethod

qiskit_algorithms/gradients/base/base_qgt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def run(
161161
opts = copy(self._default_options)
162162
opts.update_options(**options)
163163
job = AlgorithmJob(self._run, circuits, parameter_values, parameters, **opts.__dict__)
164-
job.submit()
164+
job._submit()
165165
return job
166166

167167
@abstractmethod

qiskit_algorithms/gradients/base/base_sampler_gradient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def run(
107107
opts = copy(self._default_options)
108108
opts.update_options(**options)
109109
job = AlgorithmJob(self._run, circuits, parameter_values, parameters, **opts.__dict__)
110-
job.submit()
110+
job._submit()
111111
return job
112112

113113
@abstractmethod

qiskit_algorithms/gradients/qfi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def run(
103103
opts = copy(self._default_options)
104104
opts.update_options(**options)
105105
job = AlgorithmJob(self._run, circuits, parameter_values, parameters, **opts.__dict__)
106-
job.submit()
106+
job._submit()
107107
return job
108108

109109
def _run(

qiskit_algorithms/minimum_eigensolvers/diagonal_estimator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _run(
104104
job = AlgorithmJob(
105105
self._call, circuit_indices, observable_indices, parameter_values, **run_options
106106
)
107-
job.submit()
107+
job._submit()
108108
return job
109109

110110
def _call(

qiskit_algorithms/phase_estimators/hamiltonian_phase_estimation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
from __future__ import annotations
1616

17+
from typing import Any
18+
1719
from qiskit import QuantumCircuit
1820
from qiskit.circuit.library import PauliEvolutionGate
1921
from qiskit.primitives import BaseSamplerV2
@@ -85,6 +87,7 @@ def __init__(
8587
num_evaluation_qubits: int,
8688
sampler: BaseSamplerV2 | None = None,
8789
transpiler: Transpiler | None = None,
90+
transpiler_options: dict[str, Any] | None = None,
8891
) -> None:
8992
r"""
9093
Args:
@@ -94,11 +97,14 @@ def __init__(
9497
transpiler: An optional object with a `run` method allowing to transpile the circuits
9598
that are produced within this algorithm. If set to `None`, these won't be
9699
transpiled.
100+
transpiler_options: A dictionary of options to be passed to the transpiler's `run`
101+
method as keyword arguments.
97102
"""
98103
self._phase_estimation = PhaseEstimation(
99104
num_evaluation_qubits=num_evaluation_qubits,
100105
sampler=sampler,
101106
transpiler=transpiler,
107+
transpiler_options=transpiler_options
102108
)
103109

104110
def _get_scale(self, hamiltonian, bound=None) -> PhaseEstimationScale:

qiskit_algorithms/phase_estimators/phase_estimation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
from __future__ import annotations
1717

18+
from typing import Any
19+
1820
import numpy
1921
import qiskit
2022
from qiskit import circuit
@@ -85,6 +87,7 @@ def __init__(
8587
num_evaluation_qubits: int,
8688
sampler: BaseSamplerV2 | None = None,
8789
transpiler: Transpiler | None = None,
90+
transpiler_options: dict[str, Any] | None = None,
8891
) -> None:
8992
r"""
9093
Args:
@@ -94,6 +97,8 @@ def __init__(
9497
transpiler: An optional object with a `run` method allowing to transpile the circuits
9598
that are produced within this algorithm. If set to `None`, these won't be
9699
transpiled.
100+
transpiler_options: A dictionary of options to be passed to the transpiler's `run`
101+
method as keyword arguments.
97102
98103
Raises:
99104
AlgorithmError: If a sampler is not provided

qiskit_algorithms/state_fidelities/base_state_fidelity.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
from __future__ import annotations
1717
from abc import ABC, abstractmethod
1818
from collections.abc import MutableMapping
19-
from typing import cast, Sequence, List
19+
from typing import cast, Sequence, List, Any
2020
import numpy as np
2121

2222
from qiskit import QuantumCircuit
2323
from qiskit.circuit import ParameterVector
2424
from qiskit.primitives.utils import _circuit_key
2525

2626
from ..algorithm_job import AlgorithmJob
27+
from ..custom_types import Transpiler
2728

2829

2930
class BaseStateFidelity(ABC):
@@ -42,10 +43,19 @@ class BaseStateFidelity(ABC):
4243
4344
"""
4445

45-
def __init__(self) -> None:
46-
46+
def __init__(self, transpiler: Transpiler | None = None, transpiler_options: dict[str, Any] | None = None,) -> None:
47+
r"""
48+
Args:
49+
transpiler: An optional object with a `run` method allowing to transpile the circuits
50+
that are produced within this algorithm. If set to `None`, these won't be
51+
transpiled.
52+
transpiler_options: A dictionary of options to be passed to the transpiler's `run`
53+
method as keyword arguments.
54+
"""
4755
# use cache for preventing unnecessary circuit compositions
4856
self._circuit_cache: MutableMapping[tuple[int, int], QuantumCircuit] = {}
57+
self._transpiler = transpiler
58+
self._transpiler_options = transpiler_options if transpiler_options is not None else {}
4959

5060
@staticmethod
5161
def _preprocess_values(
@@ -195,6 +205,9 @@ def _construct_circuits(
195205
# update cache
196206
self._circuit_cache[_circuit_key(circuit_1), _circuit_key(circuit_2)] = circuit
197207

208+
if self._transpiler is not None:
209+
return self._transpiler.run(circuits, **self._transpiler_options)
210+
198211
return circuits
199212

200213
def _construct_value_list(
@@ -245,7 +258,7 @@ def _run(
245258
circuits_2: QuantumCircuit | Sequence[QuantumCircuit],
246259
values_1: Sequence[float] | Sequence[Sequence[float]] | None = None,
247260
values_2: Sequence[float] | Sequence[Sequence[float]] | None = None,
248-
**options,
261+
shots: int | Sequence[int] | None = None,
249262
) -> AlgorithmJob:
250263
r"""
251264
Computes the state overlap (fidelity) calculation between two
@@ -257,10 +270,12 @@ def _run(
257270
circuits_2: (Parametrized) quantum circuits preparing :math:`|\phi\rangle`.
258271
values_1: Numerical parameters to be bound to the first set of circuits
259272
values_2: Numerical parameters to be bound to the second set of circuits.
260-
options: Primitive backend runtime options used for circuit execution. The order
261-
of priority is\: options in ``run`` method > fidelity's default
262-
options > primitive's default setting.
263-
Higher priority setting overrides lower priority setting.
273+
shots: Number of shots to be used by the underlying sampler. If a single integer is
274+
provided, this number will be used for all circuits. If a sequence of integers is
275+
provided, they will be used on a per-circuit basis. If none is provided, the
276+
fidelity's default number of shots will be used for all circuits. If this number is
277+
also set to None, the underlying primitive's default number of shots will be used
278+
for all circuits.
264279
265280
Returns:
266281
A newly constructed algorithm job instance to get the fidelity result.
@@ -273,7 +288,7 @@ def run(
273288
circuits_2: QuantumCircuit | Sequence[QuantumCircuit],
274289
values_1: Sequence[float] | Sequence[Sequence[float]] | None = None,
275290
values_2: Sequence[float] | Sequence[Sequence[float]] | None = None,
276-
**options,
291+
shots: int | Sequence[int] | None = None,
277292
) -> AlgorithmJob:
278293
r"""
279294
Runs asynchronously the state overlap (fidelity) calculation between two
@@ -286,18 +301,20 @@ def run(
286301
circuits_2: (Parametrized) quantum circuits preparing :math:`|\phi\rangle`.
287302
values_1: Numerical parameters to be bound to the first set of circuits.
288303
values_2: Numerical parameters to be bound to the second set of circuits.
289-
options: Primitive backend runtime options used for circuit execution. The order
290-
of priority is\: options in ``run`` method > fidelity's default
291-
options > primitive's default setting.
292-
Higher priority setting overrides lower priority setting.
304+
shots: Number of shots to be used by the underlying sampler. If a single integer is
305+
provided, this number will be used for all circuits. If a sequence of integers is
306+
provided, they will be used on a per-circuit basis. If none is provided, the
307+
fidelity's default number of shots will be used for all circuits. If this number is
308+
also set to None, the underlying primitive's default number of shots will be used
309+
for all circuits.
293310
294311
Returns:
295312
Primitive job for the fidelity calculation.
296313
The job's result is an instance of :class:`.StateFidelityResult`.
297314
"""
298-
job = self._run(circuits_1, circuits_2, values_1, values_2, **options)
315+
job = self._run(circuits_1, circuits_2, values_1, values_2, shots)
299316

300-
job.submit()
317+
job._submit()
301318
return job
302319

303320
@staticmethod

0 commit comments

Comments
 (0)