Skip to content

Commit 3e987cd

Browse files
ElePTCryoris
andcommitted
Deprecate accidentally public internal helper methods on QuantumCircuit (#12785)
* Deprecate accidentally public qc helper methods * Fix lint * Complete reno and add removal timeline * Update qiskit/circuit/quantumcircuit.py Co-authored-by: Julien Gacon <[email protected]> * Update remaining docstrings and removal timeline. * Trailing whitespace --------- Co-authored-by: Julien Gacon <[email protected]>
1 parent a46208c commit 3e987cd

File tree

5 files changed

+115
-24
lines changed

5 files changed

+115
-24
lines changed

qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def mcrx(
227227
"""
228228
from .rx import RXGate
229229

230-
control_qubits = self.qbit_argument_conversion(q_controls)
231-
target_qubit = self.qbit_argument_conversion(q_target)
230+
control_qubits = self._qbit_argument_conversion(q_controls)
231+
target_qubit = self._qbit_argument_conversion(q_target)
232232
if len(target_qubit) != 1:
233233
raise QiskitError("The mcrz gate needs a single qubit as target.")
234234
all_qubits = control_qubits + target_qubit
@@ -292,11 +292,11 @@ def mcry(
292292
"""
293293
from .ry import RYGate
294294

295-
control_qubits = self.qbit_argument_conversion(q_controls)
296-
target_qubit = self.qbit_argument_conversion(q_target)
295+
control_qubits = self._qbit_argument_conversion(q_controls)
296+
target_qubit = self._qbit_argument_conversion(q_target)
297297
if len(target_qubit) != 1:
298298
raise QiskitError("The mcrz gate needs a single qubit as target.")
299-
ancillary_qubits = [] if q_ancillae is None else self.qbit_argument_conversion(q_ancillae)
299+
ancillary_qubits = [] if q_ancillae is None else self._qbit_argument_conversion(q_ancillae)
300300
all_qubits = control_qubits + target_qubit + ancillary_qubits
301301
target_qubit = target_qubit[0]
302302
self._check_dups(all_qubits)
@@ -365,8 +365,8 @@ def mcrz(
365365
"""
366366
from .rz import CRZGate, RZGate
367367

368-
control_qubits = self.qbit_argument_conversion(q_controls)
369-
target_qubit = self.qbit_argument_conversion(q_target)
368+
control_qubits = self._qbit_argument_conversion(q_controls)
369+
target_qubit = self._qbit_argument_conversion(q_target)
370370
if len(target_qubit) != 1:
371371
raise QiskitError("The mcrz gate needs a single qubit as target.")
372372
all_qubits = control_qubits + target_qubit

qiskit/circuit/quantumcircuit.py

+93-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from qiskit.circuit.gate import Gate
4545
from qiskit.circuit.parameter import Parameter
4646
from qiskit.circuit.exceptions import CircuitError
47+
from qiskit.utils import deprecate_func
4748
from . import _classical_resource_map
4849
from ._utils import sort_parameters
4950
from .controlflow import ControlFlowOp, _builder_utils
@@ -1077,7 +1078,7 @@ def __init__(
10771078
self.name: str
10781079
"""A human-readable name for the circuit."""
10791080
if name is None:
1080-
self._base_name = self.cls_prefix()
1081+
self._base_name = self._cls_prefix()
10811082
self._name_update()
10821083
elif not isinstance(name, str):
10831084
raise CircuitError(
@@ -1400,24 +1401,47 @@ def _increment_instances(cls):
14001401
cls.instances += 1
14011402

14021403
@classmethod
1404+
@deprecate_func(
1405+
since=1.2,
1406+
removal_timeline="in the 2.0 release",
1407+
additional_msg="This method is only used as an internal helper "
1408+
"and will be removed with no replacement.",
1409+
)
14031410
def cls_instances(cls) -> int:
14041411
"""Return the current number of instances of this class,
14051412
useful for auto naming."""
14061413
return cls.instances
14071414

14081415
@classmethod
1416+
def _cls_instances(cls) -> int:
1417+
"""Return the current number of instances of this class,
1418+
useful for auto naming."""
1419+
return cls.instances
1420+
1421+
@classmethod
1422+
@deprecate_func(
1423+
since=1.2,
1424+
removal_timeline="in the 2.0 release",
1425+
additional_msg="This method is only used as an internal helper "
1426+
"and will be removed with no replacement.",
1427+
)
14091428
def cls_prefix(cls) -> str:
14101429
"""Return the prefix to use for auto naming."""
14111430
return cls.prefix
14121431

1432+
@classmethod
1433+
def _cls_prefix(cls) -> str:
1434+
"""Return the prefix to use for auto naming."""
1435+
return cls.prefix
1436+
14131437
def _name_update(self) -> None:
14141438
"""update name of instance using instance number"""
14151439
if not is_main_process():
14161440
pid_name = f"-{mp.current_process().pid}"
14171441
else:
14181442
pid_name = ""
14191443

1420-
self.name = f"{self._base_name}-{self.cls_instances()}{pid_name}"
1444+
self.name = f"{self._base_name}-{self._cls_instances()}{pid_name}"
14211445

14221446
def has_register(self, register: Register) -> bool:
14231447
"""
@@ -1926,7 +1950,7 @@ def replace_var(var: expr.Var, cache: Mapping[expr.Var, expr.Var]) -> expr.Var:
19261950
mapped_qubits = dest.qubits
19271951
edge_map.update(zip(other.qubits, dest.qubits))
19281952
else:
1929-
mapped_qubits = dest.qbit_argument_conversion(qubits)
1953+
mapped_qubits = dest._qbit_argument_conversion(qubits)
19301954
if len(mapped_qubits) != other.num_qubits:
19311955
raise CircuitError(
19321956
f"Number of items in qubits parameter ({len(mapped_qubits)}) does not"
@@ -1942,7 +1966,7 @@ def replace_var(var: expr.Var, cache: Mapping[expr.Var, expr.Var]) -> expr.Var:
19421966
mapped_clbits = dest.clbits
19431967
edge_map.update(zip(other.clbits, dest.clbits))
19441968
else:
1945-
mapped_clbits = dest.cbit_argument_conversion(clbits)
1969+
mapped_clbits = dest._cbit_argument_conversion(clbits)
19461970
if len(mapped_clbits) != other.num_clbits:
19471971
raise CircuitError(
19481972
f"Number of items in clbits parameter ({len(mapped_clbits)}) does not"
@@ -1952,7 +1976,7 @@ def replace_var(var: expr.Var, cache: Mapping[expr.Var, expr.Var]) -> expr.Var:
19521976
raise CircuitError(
19531977
f"Duplicate clbits referenced in 'clbits' parameter: '{mapped_clbits}'"
19541978
)
1955-
edge_map.update(zip(other.clbits, dest.cbit_argument_conversion(clbits)))
1979+
edge_map.update(zip(other.clbits, dest._cbit_argument_conversion(clbits)))
19561980

19571981
for gate, cals in other.calibrations.items():
19581982
dest._calibrations[gate].update(cals)
@@ -2267,38 +2291,91 @@ def __getitem__(self, item):
22672291
return self._data[item]
22682292

22692293
@staticmethod
2294+
@deprecate_func(
2295+
since=1.2,
2296+
removal_timeline="in the 2.0 release",
2297+
additional_msg="This method is only used as an internal helper "
2298+
"and will be removed with no replacement.",
2299+
)
22702300
def cast(value: S, type_: Callable[..., T]) -> Union[S, T]:
22712301
"""Best effort to cast value to type. Otherwise, returns the value."""
22722302
try:
22732303
return type_(value)
22742304
except (ValueError, TypeError):
22752305
return value
22762306

2307+
@staticmethod
2308+
def _cast(value: S, type_: Callable[..., T]) -> Union[S, T]:
2309+
"""Best effort to cast value to type. Otherwise, returns the value."""
2310+
try:
2311+
return type_(value)
2312+
except (ValueError, TypeError):
2313+
return value
2314+
2315+
@deprecate_func(
2316+
since=1.2,
2317+
removal_timeline="in the 2.0 release",
2318+
additional_msg="This method is only used as an internal helper "
2319+
"and will be removed with no replacement.",
2320+
)
22772321
def qbit_argument_conversion(self, qubit_representation: QubitSpecifier) -> list[Qubit]:
22782322
"""
22792323
Converts several qubit representations (such as indexes, range, etc.)
22802324
into a list of qubits.
22812325
22822326
Args:
2283-
qubit_representation (Object): representation to expand
2327+
qubit_representation: Representation to expand.
22842328
22852329
Returns:
2286-
List(Qubit): the resolved instances of the qubits.
2330+
The resolved instances of the qubits.
2331+
"""
2332+
2333+
return self._qbit_argument_conversion(qubit_representation)
2334+
2335+
def _qbit_argument_conversion(self, qubit_representation: QubitSpecifier) -> list[Qubit]:
2336+
"""
2337+
Converts several qubit representations (such as indexes, range, etc.)
2338+
into a list of qubits.
2339+
2340+
Args:
2341+
qubit_representation: Representation to expand.
2342+
2343+
Returns:
2344+
The resolved instances of the qubits.
22872345
"""
22882346
return _bit_argument_conversion(
22892347
qubit_representation, self.qubits, self._qubit_indices, Qubit
22902348
)
22912349

2350+
@deprecate_func(
2351+
since=1.2,
2352+
removal_timeline="in the 2.0 release",
2353+
additional_msg="This method is only used as an internal helper "
2354+
"and will be removed with no replacement.",
2355+
)
22922356
def cbit_argument_conversion(self, clbit_representation: ClbitSpecifier) -> list[Clbit]:
22932357
"""
22942358
Converts several classical bit representations (such as indexes, range, etc.)
22952359
into a list of classical bits.
22962360
22972361
Args:
2298-
clbit_representation (Object): representation to expand
2362+
clbit_representation : Representation to expand.
22992363
23002364
Returns:
2301-
List(tuple): Where each tuple is a classical bit.
2365+
A list of tuples where each tuple is a classical bit.
2366+
"""
2367+
return self._cbit_argument_conversion(clbit_representation)
2368+
2369+
def _cbit_argument_conversion(self, clbit_representation: ClbitSpecifier) -> list[Clbit]:
2370+
"""
2371+
Converts several classical bit representations (such as indexes, range, etc.)
2372+
into a list of classical bits.
2373+
2374+
Args:
2375+
clbit_representation: Representation to expand.
2376+
2377+
Returns:
2378+
A list of tuples where each tuple is a classical bit.
23022379
"""
23032380
return _bit_argument_conversion(
23042381
clbit_representation, self.clbits, self._clbit_indices, Clbit
@@ -2317,7 +2394,7 @@ def _append_standard_gate(
23172394
if params is None:
23182395
params = []
23192396

2320-
expanded_qargs = [self.qbit_argument_conversion(qarg) for qarg in qargs or []]
2397+
expanded_qargs = [self._qbit_argument_conversion(qarg) for qarg in qargs or []]
23212398
for param in params:
23222399
Gate.validate_parameter(op, param)
23232400

@@ -2416,8 +2493,8 @@ def append(
24162493
" which are not in this circuit"
24172494
)
24182495

2419-
expanded_qargs = [self.qbit_argument_conversion(qarg) for qarg in qargs or []]
2420-
expanded_cargs = [self.cbit_argument_conversion(carg) for carg in cargs or []]
2496+
expanded_qargs = [self._qbit_argument_conversion(qarg) for qarg in qargs or []]
2497+
expanded_cargs = [self._cbit_argument_conversion(carg) for carg in cargs or []]
24212498

24222499
instructions = InstructionSet(resource_requester=circuit_scope.resolve_classical_resource)
24232500
# For Operations that are non-Instructions, we use the Instruction's default method
@@ -4416,7 +4493,9 @@ def barrier(self, *qargs: QubitSpecifier, label=None) -> InstructionSet:
44164493

44174494
if qargs:
44184495
# This uses a `dict` not a `set` to guarantee a deterministic order to the arguments.
4419-
qubits = tuple({q: None for qarg in qargs for q in self.qbit_argument_conversion(qarg)})
4496+
qubits = tuple(
4497+
{q: None for qarg in qargs for q in self._qbit_argument_conversion(qarg)}
4498+
)
44204499
return self.append(
44214500
CircuitInstruction(Barrier(len(qubits), label=label), qubits, ()), copy=False
44224501
)
@@ -5448,7 +5527,7 @@ def mcx(
54485527

54495528
# check ancilla input
54505529
if ancilla_qubits:
5451-
_ = self.qbit_argument_conversion(ancilla_qubits)
5530+
_ = self._qbit_argument_conversion(ancilla_qubits)
54525531

54535532
try:
54545533
gate = available_implementations[mode]

qiskit/circuit/quantumcircuitdata.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def _resolve_legacy_value(self, operation, qargs, cargs) -> CircuitInstruction:
5353
if not isinstance(operation, Operation):
5454
raise CircuitError("object is not an Operation.")
5555

56-
expanded_qargs = [self._circuit.qbit_argument_conversion(qarg) for qarg in qargs or []]
57-
expanded_cargs = [self._circuit.cbit_argument_conversion(carg) for carg in cargs or []]
56+
expanded_qargs = [self._circuit._qbit_argument_conversion(qarg) for qarg in qargs or []]
57+
expanded_cargs = [self._circuit._cbit_argument_conversion(carg) for carg in cargs or []]
5858

5959
if isinstance(operation, Instruction):
6060
broadcast_args = list(operation.broadcast_arguments(expanded_qargs, expanded_cargs))

qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from qiskit.circuit.controlflow import CONTROL_FLOW_OP_NAMES
2020
from qiskit.circuit.library.standard_gates import get_standard_gate_name_mapping
21+
from qiskit.providers.backend import Backend
2122
from qiskit.providers.backend_compat import BackendV2Converter
2223
from qiskit.transpiler.coupling import CouplingMap
2324
from qiskit.transpiler.exceptions import TranspilerError
@@ -33,7 +34,7 @@
3334

3435

3536
def generate_preset_pass_manager(
36-
optimization_level,
37+
optimization_level=2,
3738
backend=None,
3839
target=None,
3940
basis_gates=None,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
deprecations_circuits:
3+
- |
4+
The following circuit methods were not intended for public use, but were accidentally left documented in the
5+
public API during the 1.0 release. They are now deprecated from Qiskit 1.2 and will be removed in Qiskit 2.0:
6+
7+
* ``QuantumCircuit.cast``
8+
* ``QuantumCircuit.cls_instances``
9+
* ``QuantumCircuit.cls_prefix``
10+
* ``QuantumCircuit.cbit_argument_conversion``
11+
* ``QuantumCircuit.qbit_argument_conversion``

0 commit comments

Comments
 (0)