Skip to content

Commit

Permalink
Revert "make circuit_key public"
Browse files Browse the repository at this point in the history
This reverts commit 2923245.
  • Loading branch information
ikkoham committed Jan 18, 2023
1 parent 8990305 commit a02605e
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 53 deletions.
12 changes: 6 additions & 6 deletions qiskit/algorithms/gradients/base_estimator_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from qiskit.circuit import Parameter, ParameterExpression, QuantumCircuit
from qiskit.opflow import PauliSumOp
from qiskit.primitives import BaseEstimator
from qiskit.primitives.utils import circuit_key
from qiskit.primitives.utils import _circuit_key
from qiskit.providers import Options
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.transpiler.passes import TranslateParameterizedGates
Expand Down Expand Up @@ -165,11 +165,11 @@ def _preprocess(
for circuit, parameter_value_, parameter_set in zip(
circuits, parameter_values, parameter_sets
):
_circuit_key = circuit_key(circuit)
if _circuit_key not in self._gradient_circuit_cache:
circuit_key = _circuit_key(circuit)
if circuit_key not in self._gradient_circuit_cache:
unrolled = translator(circuit)
self._gradient_circuit_cache[_circuit_key] = _assign_unique_parameters(unrolled)
gradient_circuit = self._gradient_circuit_cache[_circuit_key]
self._gradient_circuit_cache[circuit_key] = _assign_unique_parameters(unrolled)
gradient_circuit = self._gradient_circuit_cache[circuit_key]
g_circuits.append(gradient_circuit.gradient_circuit)
g_parameter_values.append(
_make_gradient_parameter_values(circuit, gradient_circuit, parameter_value_)
Expand Down Expand Up @@ -208,7 +208,7 @@ def _postprocess(
):
# If the derivative type is complex, cast the gradient to complex.
unique_gradient = unique_gradient.astype("complex")
gradient_circuit = self._gradient_circuit_cache[circuit_key(circuit)]
gradient_circuit = self._gradient_circuit_cache[_circuit_key(circuit)]
g_parameter_set = _make_gradient_parameter_set(gradient_circuit, parameter_set)
# Make a map from the gradient parameter to the respective index in the gradient.
parameter_indices = [param for param in circuit.parameters if param in parameter_set]
Expand Down
12 changes: 6 additions & 6 deletions qiskit/algorithms/gradients/base_sampler_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from qiskit.algorithms import AlgorithmJob
from qiskit.circuit import Parameter, ParameterExpression, QuantumCircuit
from qiskit.primitives import BaseSampler
from qiskit.primitives.utils import circuit_key
from qiskit.primitives.utils import _circuit_key
from qiskit.providers import Options
from qiskit.transpiler.passes import TranslateParameterizedGates

Expand Down Expand Up @@ -146,11 +146,11 @@ def _preprocess(
for circuit, parameter_value_, parameter_set in zip(
circuits, parameter_values, parameter_sets
):
_circuit_key = circuit_key(circuit)
if _circuit_key not in self._gradient_circuit_cache:
circuit_key = _circuit_key(circuit)
if circuit_key not in self._gradient_circuit_cache:
unrolled = translator(circuit)
self._gradient_circuit_cache[_circuit_key] = _assign_unique_parameters(unrolled)
gradient_circuit = self._gradient_circuit_cache[_circuit_key]
self._gradient_circuit_cache[circuit_key] = _assign_unique_parameters(unrolled)
gradient_circuit = self._gradient_circuit_cache[circuit_key]
g_circuits.append(gradient_circuit.gradient_circuit)
g_parameter_values.append(
_make_gradient_parameter_values(circuit, gradient_circuit, parameter_value_)
Expand Down Expand Up @@ -182,7 +182,7 @@ def _postprocess(
for idx, (circuit, parameter_values_, parameter_set) in enumerate(
zip(circuits, parameter_values, parameter_sets)
):
gradient_circuit = self._gradient_circuit_cache[circuit_key(circuit)]
gradient_circuit = self._gradient_circuit_cache[_circuit_key(circuit)]
g_parameter_set = _make_gradient_parameter_set(gradient_circuit, parameter_set)
# Make a map from the gradient parameter to the respective index in the gradient.
parameter_indices = [param for param in circuit.parameters if param in parameter_set]
Expand Down
10 changes: 5 additions & 5 deletions qiskit/algorithms/gradients/lin_comb_estimator_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.opflow import PauliSumOp
from qiskit.primitives import BaseEstimator
from qiskit.primitives.utils import init_observable, circuit_key
from qiskit.primitives.utils import init_observable, _circuit_key
from qiskit.providers import Options
from qiskit.quantum_info import SparsePauliOp
from qiskit.quantum_info.operators.base_operator import BaseOperator
Expand Down Expand Up @@ -124,12 +124,12 @@ def _run_unique(
):
# Prepare circuits for the gradient of the specified parameters.
meta = {"parameters": [p for p in circuit.parameters if p in parameter_set]}
_circuit_key = circuit_key(circuit)
if _circuit_key not in self._lin_comb_cache:
self._lin_comb_cache[_circuit_key] = _make_lin_comb_gradient_circuit(
circuit_key = _circuit_key(circuit)
if circuit_key not in self._lin_comb_cache:
self._lin_comb_cache[circuit_key] = _make_lin_comb_gradient_circuit(
circuit, add_measurement=False
)
lin_comb_circuits = self._lin_comb_cache[_circuit_key]
lin_comb_circuits = self._lin_comb_cache[circuit_key]
gradient_circuits = []
for param in circuit.parameters:
if param not in parameter_set:
Expand Down
6 changes: 3 additions & 3 deletions qiskit/algorithms/gradients/lin_comb_qfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit.algorithms import AlgorithmError
from qiskit.circuit import Parameter, ParameterExpression, QuantumCircuit
from qiskit.primitives import BaseEstimator
from qiskit.primitives.utils import circuit_key
from qiskit.primitives.utils import _circuit_key
from qiskit.providers import Options
from qiskit.quantum_info import SparsePauliOp

Expand Down Expand Up @@ -138,13 +138,13 @@ def _run(
)
gradient_jobs.append(gradient_job)
# compute the first term in the QFI
qfi_circuits_ = self._qfi_circuit_cache.get(circuit_key(circuit))
qfi_circuits_ = self._qfi_circuit_cache.get(_circuit_key(circuit))
if qfi_circuits_ is None:
# generate the all of the circuits for the first term in the QFI and cache them.
# only the circuit related to specified parameters will be executed.
# In the future, we can generate the specified circuits on demand.
qfi_circuits_ = _make_lin_comb_qfi_circuit(circuit)
self._qfi_circuit_cache[circuit_key(circuit)] = qfi_circuits_
self._qfi_circuit_cache[_circuit_key(circuit)] = qfi_circuits_

# only compute the gradients for parameters in the parameter set
qfi_circuits, result_indices, coeffs = [], [], []
Expand Down
10 changes: 5 additions & 5 deletions qiskit/algorithms/gradients/lin_comb_sampler_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from qiskit.algorithms import AlgorithmError
from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.primitives import BaseSampler
from qiskit.primitives.utils import circuit_key
from qiskit.primitives.utils import _circuit_key
from qiskit.providers import Options

from .base_sampler_gradient import BaseSamplerGradient
Expand Down Expand Up @@ -103,12 +103,12 @@ def _run_unique(
# Prepare circuits for the gradient of the specified parameters.
meta = {"parameters": [p for p in circuit.parameters if p in parameter_set]}
metadata.append(meta)
_circuit_key = circuit_key(circuit)
if _circuit_key not in self._lin_comb_cache:
self._lin_comb_cache[_circuit_key] = _make_lin_comb_gradient_circuit(
circuit_key = _circuit_key(circuit)
if circuit_key not in self._lin_comb_cache:
self._lin_comb_cache[circuit_key] = _make_lin_comb_gradient_circuit(
circuit, add_measurement=True
)
lin_comb_circuits = self._lin_comb_cache[_circuit_key]
lin_comb_circuits = self._lin_comb_cache[circuit_key]
gradient_circuits = []
for param in circuit.parameters:
if param not in parameter_set:
Expand Down
4 changes: 2 additions & 2 deletions qiskit/algorithms/minimum_eigensolvers/diagonal_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qiskit.algorithms.algorithm_job import AlgorithmJob
from qiskit.circuit import QuantumCircuit
from qiskit.primitives import BaseSampler, BaseEstimator, EstimatorResult
from qiskit.primitives.utils import init_observable, circuit_key
from qiskit.primitives.utils import init_observable, _circuit_key
from qiskit.opflow import PauliSumOp
from qiskit.quantum_info import SparsePauliOp
from qiskit.quantum_info.operators.base_operator import BaseOperator
Expand Down Expand Up @@ -75,7 +75,7 @@ def _run(
) -> AlgorithmJob:
circuit_indices = []
for circuit in circuits:
key = circuit_key(circuit)
key = _circuit_key(circuit)
index = self._circuit_ids.get(key)
if index is not None:
circuit_indices.append(index)
Expand Down
6 changes: 3 additions & 3 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from .base import BaseEstimator, EstimatorResult
from .primitive_job import PrimitiveJob
from .utils import circuit_key, _observable_key, init_observable
from .utils import _circuit_key, _observable_key, init_observable


def _run_circuits(
Expand Down Expand Up @@ -267,12 +267,12 @@ def _run(
) -> PrimitiveJob:
circuit_indices = []
for circuit in circuits:
index = self._circuit_ids.get(circuit_key(circuit))
index = self._circuit_ids.get(_circuit_key(circuit))
if index is not None:
circuit_indices.append(index)
else:
circuit_indices.append(len(self._circuits))
self._circuit_ids[circuit_key(circuit)] = len(self._circuits)
self._circuit_ids[_circuit_key(circuit)] = len(self._circuits)
self._circuits.append(circuit)
self._parameters.append(circuit.parameters)
observable_indices = []
Expand Down
6 changes: 3 additions & 3 deletions qiskit/primitives/backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from .backend_estimator import _prepare_counts, _run_circuits
from .base import BaseSampler, SamplerResult
from .primitive_job import PrimitiveJob
from .utils import circuit_key
from .utils import _circuit_key


class BackendSampler(BaseSampler):
Expand Down Expand Up @@ -197,12 +197,12 @@ def _run(
) -> PrimitiveJob:
circuit_indices = []
for circuit in circuits:
index = self._circuit_ids.get(circuit_key(circuit))
index = self._circuit_ids.get(_circuit_key(circuit))
if index is not None:
circuit_indices.append(index)
else:
circuit_indices.append(len(self._circuits))
self._circuit_ids[circuit_key(circuit)] = len(self._circuits)
self._circuit_ids[_circuit_key(circuit)] = len(self._circuits)
self._circuits.append(circuit)
self._parameters.append(circuit.parameters)
job = PrimitiveJob(self._call, circuit_indices, parameter_values, **run_options)
Expand Down
8 changes: 4 additions & 4 deletions qiskit/primitives/base/base_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@

from .base_primitive import BasePrimitive
from .estimator_result import EstimatorResult
from ..utils import circuit_key, _observable_key, init_observable
from ..utils import _circuit_key, _observable_key, init_observable


class BaseEstimator(BasePrimitive):
Expand Down Expand Up @@ -297,9 +297,9 @@ def __new__(
self._circuit_ids = {}
elif isinstance(circuits, Iterable):
circuits = copy(circuits)
self._circuit_ids = {circuit_key(circuit): i for i, circuit in enumerate(circuits)}
self._circuit_ids = {_circuit_key(circuit): i for i, circuit in enumerate(circuits)}
else:
self._circuit_ids = {circuit_key(circuits): 0}
self._circuit_ids = {_circuit_key(circuits): 0}
if observables is None:
self._observable_ids = {}
elif isinstance(observables, Iterable):
Expand Down Expand Up @@ -413,7 +413,7 @@ def __call__(

# Allow objects
circuits = [
self._circuit_ids.get(circuit_key(circuit))
self._circuit_ids.get(_circuit_key(circuit))
if not isinstance(circuit, (int, np.integer))
else circuit
for circuit in circuits
Expand Down
8 changes: 4 additions & 4 deletions qiskit/primitives/base/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

from .base_primitive import BasePrimitive
from .sampler_result import SamplerResult
from ..utils import circuit_key
from ..utils import _circuit_key


class BaseSampler(BasePrimitive):
Expand Down Expand Up @@ -233,9 +233,9 @@ def __new__(
self._circuit_ids = {}
elif isinstance(circuits, Iterable):
circuits = copy(circuits)
self._circuit_ids = {circuit_key(circuit): i for i, circuit in enumerate(circuits)}
self._circuit_ids = {_circuit_key(circuit): i for i, circuit in enumerate(circuits)}
else:
self._circuit_ids = {circuit_key(circuits): 0}
self._circuit_ids = {_circuit_key(circuits): 0}
return self

@property
Expand Down Expand Up @@ -310,7 +310,7 @@ def __call__(

# Allow objects
circuits = [
self._circuit_ids.get(circuit_key(circuit))
self._circuit_ids.get(_circuit_key(circuit))
if not isinstance(circuit, (int, np.integer))
else circuit
for circuit in circuits
Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from .base import BaseEstimator, EstimatorResult
from .primitive_job import PrimitiveJob
from .utils import (
circuit_key,
_circuit_key,
_observable_key,
bound_circuit_to_instruction,
init_circuit,
Expand Down Expand Up @@ -161,7 +161,7 @@ def _run(
) -> PrimitiveJob:
circuit_indices = []
for circuit in circuits:
key = circuit_key(circuit)
key = _circuit_key(circuit)
index = self._circuit_ids.get(key)
if index is not None:
circuit_indices.append(index)
Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .base import BaseSampler, SamplerResult
from .primitive_job import PrimitiveJob
from .utils import (
circuit_key,
_circuit_key,
bound_circuit_to_instruction,
final_measurement_mapping,
init_circuit,
Expand Down Expand Up @@ -142,7 +142,7 @@ def _run(
) -> PrimitiveJob:
circuit_indices = []
for circuit in circuits:
key = circuit_key(circuit)
key = _circuit_key(circuit)
index = self._circuit_ids.get(key)
if index is not None:
circuit_indices.append(index)
Expand Down
6 changes: 3 additions & 3 deletions qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ def _format_params(param):
if isinstance(param, np.ndarray):
return param.data.tobytes()
elif isinstance(param, QuantumCircuit):
return circuit_key(param)
return _circuit_key(param)
elif isinstance(param, Iterable):
return tuple(param)
return param


def circuit_key(circuit: QuantumCircuit, functional: bool = True) -> tuple:
"""Hashable serializable key function for QuantumCircuit.
def _circuit_key(circuit: QuantumCircuit, functional: bool = True) -> tuple:
"""Private key function for QuantumCircuit.
This is the workaround until :meth:`QuantumCircuit.__hash__` will be introduced.
If key collision is found, please add elements to avoid it.
Expand Down
10 changes: 5 additions & 5 deletions test/python/primitives/test_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from qiskit import QuantumCircuit, pulse, transpile
from qiskit.circuit.random import random_circuit
from qiskit.primitives.base.base_primitive import BasePrimitive
from qiskit.primitives.utils import circuit_key
from qiskit.primitives.utils import _circuit_key
from qiskit.providers.fake_provider import FakeAlmaden
from qiskit.test import QiskitTestCase

Expand Down Expand Up @@ -129,7 +129,7 @@ def test_func(n):
qc.ry(n, 0)
return qc

keys = [circuit_key(test_func(i)) for i in range(5)]
keys = [_circuit_key(test_func(i)) for i in range(5)]
self.assertEqual(len(keys), len(set(keys)))

with self.subTest("pulse circuit"):
Expand All @@ -144,7 +144,7 @@ def test_with_scheduling(n):
qc.add_calibration("x", qubits=(0,), schedule=custom_gate)
return transpile(qc, FakeAlmaden(), scheduling_method="alap")

keys = [circuit_key(test_with_scheduling(i)) for i in range(1, 5)]
keys = [_circuit_key(test_with_scheduling(i)) for i in range(1, 5)]
self.assertEqual(len(keys), len(set(keys)))

def test_circuit_key_controlflow(self):
Expand All @@ -157,5 +157,5 @@ def test_circuit_key_controlflow(self):
qc.measure(0, 0)
qc.break_loop().c_if(0, True)

self.assertIsInstance(hash(circuit_key(qc)), int)
self.assertIsInstance(json.dumps(circuit_key(qc)), str)
self.assertIsInstance(hash(_circuit_key(qc)), int)
self.assertIsInstance(json.dumps(_circuit_key(qc)), str)

0 comments on commit a02605e

Please sign in to comment.