44
44
from qiskit .circuit .gate import Gate
45
45
from qiskit .circuit .parameter import Parameter
46
46
from qiskit .circuit .exceptions import CircuitError
47
+ from qiskit .utils import deprecate_func
47
48
from . import _classical_resource_map
48
49
from ._utils import sort_parameters
49
50
from .controlflow import ControlFlowOp , _builder_utils
@@ -1077,7 +1078,7 @@ def __init__(
1077
1078
self .name : str
1078
1079
"""A human-readable name for the circuit."""
1079
1080
if name is None :
1080
- self ._base_name = self .cls_prefix ()
1081
+ self ._base_name = self ._cls_prefix ()
1081
1082
self ._name_update ()
1082
1083
elif not isinstance (name , str ):
1083
1084
raise CircuitError (
@@ -1400,24 +1401,47 @@ def _increment_instances(cls):
1400
1401
cls .instances += 1
1401
1402
1402
1403
@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
+ )
1403
1410
def cls_instances (cls ) -> int :
1404
1411
"""Return the current number of instances of this class,
1405
1412
useful for auto naming."""
1406
1413
return cls .instances
1407
1414
1408
1415
@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
+ )
1409
1428
def cls_prefix (cls ) -> str :
1410
1429
"""Return the prefix to use for auto naming."""
1411
1430
return cls .prefix
1412
1431
1432
+ @classmethod
1433
+ def _cls_prefix (cls ) -> str :
1434
+ """Return the prefix to use for auto naming."""
1435
+ return cls .prefix
1436
+
1413
1437
def _name_update (self ) -> None :
1414
1438
"""update name of instance using instance number"""
1415
1439
if not is_main_process ():
1416
1440
pid_name = f"-{ mp .current_process ().pid } "
1417
1441
else :
1418
1442
pid_name = ""
1419
1443
1420
- self .name = f"{ self ._base_name } -{ self .cls_instances ()} { pid_name } "
1444
+ self .name = f"{ self ._base_name } -{ self ._cls_instances ()} { pid_name } "
1421
1445
1422
1446
def has_register (self , register : Register ) -> bool :
1423
1447
"""
@@ -1926,7 +1950,7 @@ def replace_var(var: expr.Var, cache: Mapping[expr.Var, expr.Var]) -> expr.Var:
1926
1950
mapped_qubits = dest .qubits
1927
1951
edge_map .update (zip (other .qubits , dest .qubits ))
1928
1952
else :
1929
- mapped_qubits = dest .qbit_argument_conversion (qubits )
1953
+ mapped_qubits = dest ._qbit_argument_conversion (qubits )
1930
1954
if len (mapped_qubits ) != other .num_qubits :
1931
1955
raise CircuitError (
1932
1956
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:
1942
1966
mapped_clbits = dest .clbits
1943
1967
edge_map .update (zip (other .clbits , dest .clbits ))
1944
1968
else :
1945
- mapped_clbits = dest .cbit_argument_conversion (clbits )
1969
+ mapped_clbits = dest ._cbit_argument_conversion (clbits )
1946
1970
if len (mapped_clbits ) != other .num_clbits :
1947
1971
raise CircuitError (
1948
1972
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:
1952
1976
raise CircuitError (
1953
1977
f"Duplicate clbits referenced in 'clbits' parameter: '{ mapped_clbits } '"
1954
1978
)
1955
- edge_map .update (zip (other .clbits , dest .cbit_argument_conversion (clbits )))
1979
+ edge_map .update (zip (other .clbits , dest ._cbit_argument_conversion (clbits )))
1956
1980
1957
1981
for gate , cals in other .calibrations .items ():
1958
1982
dest ._calibrations [gate ].update (cals )
@@ -2267,38 +2291,91 @@ def __getitem__(self, item):
2267
2291
return self ._data [item ]
2268
2292
2269
2293
@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
+ )
2270
2300
def cast (value : S , type_ : Callable [..., T ]) -> Union [S , T ]:
2271
2301
"""Best effort to cast value to type. Otherwise, returns the value."""
2272
2302
try :
2273
2303
return type_ (value )
2274
2304
except (ValueError , TypeError ):
2275
2305
return value
2276
2306
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
+ )
2277
2321
def qbit_argument_conversion (self , qubit_representation : QubitSpecifier ) -> list [Qubit ]:
2278
2322
"""
2279
2323
Converts several qubit representations (such as indexes, range, etc.)
2280
2324
into a list of qubits.
2281
2325
2282
2326
Args:
2283
- qubit_representation (Object): representation to expand
2327
+ qubit_representation: Representation to expand.
2284
2328
2285
2329
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.
2287
2345
"""
2288
2346
return _bit_argument_conversion (
2289
2347
qubit_representation , self .qubits , self ._qubit_indices , Qubit
2290
2348
)
2291
2349
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
+ )
2292
2356
def cbit_argument_conversion (self , clbit_representation : ClbitSpecifier ) -> list [Clbit ]:
2293
2357
"""
2294
2358
Converts several classical bit representations (such as indexes, range, etc.)
2295
2359
into a list of classical bits.
2296
2360
2297
2361
Args:
2298
- clbit_representation (Object): representation to expand
2362
+ clbit_representation : Representation to expand.
2299
2363
2300
2364
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.
2302
2379
"""
2303
2380
return _bit_argument_conversion (
2304
2381
clbit_representation , self .clbits , self ._clbit_indices , Clbit
@@ -2317,7 +2394,7 @@ def _append_standard_gate(
2317
2394
if params is None :
2318
2395
params = []
2319
2396
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 []]
2321
2398
for param in params :
2322
2399
Gate .validate_parameter (op , param )
2323
2400
@@ -2416,8 +2493,8 @@ def append(
2416
2493
" which are not in this circuit"
2417
2494
)
2418
2495
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 []]
2421
2498
2422
2499
instructions = InstructionSet (resource_requester = circuit_scope .resolve_classical_resource )
2423
2500
# 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:
4416
4493
4417
4494
if qargs :
4418
4495
# 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
+ )
4420
4499
return self .append (
4421
4500
CircuitInstruction (Barrier (len (qubits ), label = label ), qubits , ()), copy = False
4422
4501
)
@@ -5448,7 +5527,7 @@ def mcx(
5448
5527
5449
5528
# check ancilla input
5450
5529
if ancilla_qubits :
5451
- _ = self .qbit_argument_conversion (ancilla_qubits )
5530
+ _ = self ._qbit_argument_conversion (ancilla_qubits )
5452
5531
5453
5532
try :
5454
5533
gate = available_implementations [mode ]
0 commit comments