Skip to content

Commit

Permalink
Fix Python 3.8 compatibility
Browse files Browse the repository at this point in the history
There are some differences in how the inspect stdlib module behaves
between python 3.8 and newer versions of python. This was causing
divergence in the test and qpy behavior where inspect was used to
determine different aspects of a gate (either whether label was
supported as an arg or find the number of free parameters). This commit
fixes this by adjusting the code to handle both newer versions of
inspect as well as older ones.
  • Loading branch information
mtreinish committed Jun 20, 2023
1 parent 7087462 commit 330beb8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from qiskit.circuit import library, controlflow, CircuitInstruction
from qiskit.circuit.classicalregister import ClassicalRegister, Clbit
from qiskit.circuit.gate import Gate
from qiskit.circuit.singleton_gate import SingletonGate
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.instruction import Instruction
from qiskit.circuit.quantumcircuit import QuantumCircuit
Expand Down Expand Up @@ -292,9 +293,14 @@ def _read_instruction(file_obj, circuit, registers, custom_operations, version,
if "label" in inspect.signature(gate_class).parameters:
gate = gate_class(*params, label=label)
else:
gate = gate_class(*params)
if label is not None:
gate.label = label
if issubclass(gate_class, SingletonGate):
gate = gate_class(*params, label=label)
else:
gate = gate_class(*params)
gate.label = label
else:
gate = gate_class(*params)
if condition_tuple:
gate = gate.c_if(*condition_tuple)
if circuit is None:
Expand Down
2 changes: 1 addition & 1 deletion test/python/circuit/gate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _get_free_params(fun, ignore=None):
Returns:
list[str]: The name of the free parameters not listed in ``ignore``.
"""
ignore = ignore or []
ignore = ignore or ["kwargs"]
free_params = []
for name, param in signature(fun).parameters.items():
if param.default == Parameter.empty and param.kind != Parameter.VAR_POSITIONAL:
Expand Down

0 comments on commit 330beb8

Please sign in to comment.