Skip to content

Commit

Permalink
Only construction Operator from Instruction (as before) and from Clif…
Browse files Browse the repository at this point in the history
…ford (for backward compatibility)
  • Loading branch information
alexanderivrii committed Aug 10, 2022
1 parent 07733d6 commit ab213f2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions qiskit/quantum_info/operators/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab213f2

Please sign in to comment.