diff --git a/qiskit/quantum_info/operators/operator.py b/qiskit/quantum_info/operators/operator.py index dd5b340d8240..413731e6fe9f 100644 --- a/qiskit/quantum_info/operators/operator.py +++ b/qiskit/quantum_info/operators/operator.py @@ -21,6 +21,7 @@ import numpy as np from qiskit.circuit.quantumcircuit import QuantumCircuit +from qiskit.circuit.instruction import Instruction from qiskit.circuit.operation import Operation from qiskit.circuit.library.standard_gates import IGate, XGate, YGate, ZGate, HGate, SGate, TGate from qiskit.exceptions import QiskitError @@ -514,11 +515,16 @@ def _init_instruction(cls, instruction): @classmethod def _instruction_to_matrix(cls, obj): """Return Operator for instruction if defined or None otherwise.""" - # Note: to_matrix() is not a required method for Operations, thus we raise - # an error for opaque instructions without a matrix representation. - # On the other hand, we do use to_matrix() when it's available. - if not isinstance(obj, Operation) and not hasattr(obj, "to_matrix"): - raise QiskitError("Input is not an Operation and does not have 'to_matrix()' method.") + # Note: to_matrix() is not a required method for Operations, so for now + # we do not allow constructing matrices for general Operations. + # However, for backward compatibility we need to support constructing matrices + # for Cliffords, which happen to have a to_matrix() method. + + # pylint: disable=cyclic-import + from qiskit.quantum_info import Clifford + + if not isinstance(obj, (Instruction, Clifford)): + raise QiskitError("Input is neither an Instruction nor Clifford.") mat = None if hasattr(obj, "to_matrix"): # If instruction is a gate first we see if it has a