From bdb2aabf7fa2930a63a5255b41cb745088539a30 Mon Sep 17 00:00:00 2001 From: Tanuj Khattar Date: Fri, 13 Oct 2023 12:39:07 -0700 Subject: [PATCH 1/2] Speed up construction of single qubit pauli operations - cirq.X(q) --- cirq-core/cirq/ops/pauli_gates.py | 7 +++++-- cirq-core/cirq/ops/pauli_string.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cirq-core/cirq/ops/pauli_gates.py b/cirq-core/cirq/ops/pauli_gates.py index 3af55eef5ed..d4aa8d2aa75 100644 --- a/cirq-core/cirq/ops/pauli_gates.py +++ b/cirq-core/cirq/ops/pauli_gates.py @@ -28,6 +28,10 @@ _ZEigenState, ) # pragma: no cover +from cirq._import import LazyLoader + +pauli_string = LazyLoader("pauli_string", globals(), "cirq.ops.pauli_string") + class Pauli(raw_types.Gate, metaclass=abc.ABCMeta): """Represents the Pauli gates. @@ -97,9 +101,8 @@ def on(self, *qubits: 'cirq.Qid') -> 'SingleQubitPauliStringGateOperation': """ if len(qubits) != 1: raise ValueError(f'Expected a single qubit, got <{qubits!r}>.') - from cirq.ops.pauli_string import SingleQubitPauliStringGateOperation - return SingleQubitPauliStringGateOperation(self, qubits[0]) + return pauli_string.SingleQubitPauliStringGateOperation(self, qubits[0]) @property def _canonical_exponent(self): diff --git a/cirq-core/cirq/ops/pauli_string.py b/cirq-core/cirq/ops/pauli_string.py index 21ce7be0dac..f9e6896bffe 100644 --- a/cirq-core/cirq/ops/pauli_string.py +++ b/cirq-core/cirq/ops/pauli_string.py @@ -42,7 +42,7 @@ import sympy import cirq -from cirq import value, protocols, linalg, qis +from cirq import value, protocols, linalg, qis, _compat from cirq._doc import document from cirq._import import LazyLoader from cirq.ops import ( @@ -184,7 +184,7 @@ def __init__( Raises: TypeError: If the `qubit_pauli_map` has values that are not Paulis. """ - if qubit_pauli_map is not None: + if _compat.__cirq_debug__.get() and qubit_pauli_map is not None: for v in qubit_pauli_map.values(): if not isinstance(v, pauli_gates.Pauli): raise TypeError(f'{v} is not a Pauli') From c99efcdc90e51a77dc9f7bb36694c07d62c6472f Mon Sep 17 00:00:00 2001 From: Tanuj Khattar Date: Fri, 13 Oct 2023 12:48:36 -0700 Subject: [PATCH 2/2] Fix lint --- cirq-core/cirq/ops/pauli_gates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/pauli_gates.py b/cirq-core/cirq/ops/pauli_gates.py index d4aa8d2aa75..c7245550111 100644 --- a/cirq-core/cirq/ops/pauli_gates.py +++ b/cirq-core/cirq/ops/pauli_gates.py @@ -15,6 +15,7 @@ from typing import Any, cast, Tuple, TYPE_CHECKING, Union, Dict from cirq._doc import document +from cirq._import import LazyLoader from cirq.ops import common_gates, raw_types, identity from cirq.type_workarounds import NotImplementedType @@ -28,7 +29,6 @@ _ZEigenState, ) # pragma: no cover -from cirq._import import LazyLoader pauli_string = LazyLoader("pauli_string", globals(), "cirq.ops.pauli_string")