Skip to content

Commit a2530fe

Browse files
tanujkhattarmaffoo
andauthored
Speed up construction of single qubit pauli operations - cirq.X(q) (#6316)
Co-authored-by: Matthew Neeley <[email protected]>
1 parent 181d7aa commit a2530fe

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

cirq-core/cirq/ops/pauli_gates.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Any, cast, Tuple, TYPE_CHECKING, Union, Dict
1616

1717
from cirq._doc import document
18+
from cirq._import import LazyLoader
1819
from cirq.ops import common_gates, raw_types, identity
1920
from cirq.type_workarounds import NotImplementedType
2021

@@ -29,6 +30,9 @@
2930
) # pragma: no cover
3031

3132

33+
pauli_string = LazyLoader("pauli_string", globals(), "cirq.ops.pauli_string")
34+
35+
3236
class Pauli(raw_types.Gate, metaclass=abc.ABCMeta):
3337
"""Represents the Pauli gates.
3438
@@ -97,9 +101,8 @@ def on(self, *qubits: 'cirq.Qid') -> 'SingleQubitPauliStringGateOperation':
97101
"""
98102
if len(qubits) != 1:
99103
raise ValueError(f'Expected a single qubit, got <{qubits!r}>.')
100-
from cirq.ops.pauli_string import SingleQubitPauliStringGateOperation
101104

102-
return SingleQubitPauliStringGateOperation(self, qubits[0])
105+
return pauli_string.SingleQubitPauliStringGateOperation(self, qubits[0])
103106

104107
@property
105108
def _canonical_exponent(self):

cirq-core/cirq/ops/pauli_string.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import sympy
4343

4444
import cirq
45-
from cirq import value, protocols, linalg, qis
45+
from cirq import value, protocols, linalg, qis, _compat
4646
from cirq._doc import document
4747
from cirq._import import LazyLoader
4848
from cirq.ops import (
@@ -184,7 +184,7 @@ def __init__(
184184
Raises:
185185
TypeError: If the `qubit_pauli_map` has values that are not Paulis.
186186
"""
187-
if qubit_pauli_map is not None:
187+
if _compat.__cirq_debug__.get() and qubit_pauli_map is not None:
188188
for v in qubit_pauli_map.values():
189189
if not isinstance(v, pauli_gates.Pauli):
190190
raise TypeError(f'{v} is not a Pauli')

0 commit comments

Comments
 (0)