Skip to content
22 changes: 17 additions & 5 deletions qsimcirq/qsim_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
from typing import Dict, List, Sequence, Tuple

import cirq
Expand Down Expand Up @@ -193,19 +194,30 @@ def _translate_MeasurementGate(gate: cirq.MeasurementGate):
}


@functools.lru_cache()
def _gate_kind_translator(gate_type: type):
for t in gate_type.mro():
if t in TYPE_TRANSLATOR:
return TYPE_TRANSLATOR[t]
return None


def _is_supported_gate_type(gate_type: type):
return _gate_kind_translator(gate_type) is not None


def _cirq_gate_kind(gate: cirq.Gate):
for gate_type in type(gate).mro():
translator = TYPE_TRANSLATOR.get(gate_type, None)
if translator is not None:
return translator(gate)
translator = _gate_kind_translator(type(gate))
if translator is not None:
return translator(gate)
# Unrecognized gates will be decomposed.
return None


def _has_cirq_gate_kind(op: cirq.Operation):
if isinstance(op, cirq.ControlledOperation):
return _has_cirq_gate_kind(op.sub_operation)
return any(t in TYPE_TRANSLATOR for t in type(op.gate).mro())
return _is_supported_gate_type(type(op.gate))


def _control_details(
Expand Down
Loading