Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/codecov/codecov-ac…
Browse files Browse the repository at this point in the history
…tion-5.4.0
  • Loading branch information
pavoljuhas authored Mar 4, 2025
2 parents d442ec7 + 927f600 commit bda4094
Show file tree
Hide file tree
Showing 11 changed files with 754 additions and 23 deletions.
8 changes: 2 additions & 6 deletions cirq-core/cirq/devices/grid_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@ def __new__(cls, row: int, col: int, *, dimension: int) -> 'cirq.GridQid':
dimension: The dimension of the qid's Hilbert space, i.e.
the number of quantum levels.
"""
row = int(row)
col = int(col)
dimension = int(dimension)
key = (row, col, dimension)
inst = cls._cache.get(key)
Expand All @@ -224,7 +222,7 @@ def __new__(cls, row: int, col: int, *, dimension: int) -> 'cirq.GridQid':
inst._row = row
inst._col = col
inst._dimension = dimension
inst._hash = ((dimension - 2) * 1_000_003 + col) * 1_000_003 + row
inst._hash = ((dimension - 2) * 1_000_003 + hash(col)) * 1_000_003 + hash(row)
cls._cache[key] = inst
return inst

Expand Down Expand Up @@ -380,15 +378,13 @@ def __new__(cls, row: int, col: int) -> 'cirq.GridQubit':
row: the row coordinate
col: the column coordinate
"""
row = int(row)
col = int(col)
key = (row, col)
inst = cls._cache.get(key)
if inst is None:
inst = super().__new__(cls)
inst._row = row
inst._col = col
inst._hash = col * 1_000_003 + row
inst._hash = hash(col) * 1_000_003 + hash(row)
cls._cache[key] = inst
return inst

Expand Down
24 changes: 16 additions & 8 deletions cirq-core/cirq/devices/grid_qubit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,30 @@ def test_complex():
assert isinstance(complex(cirq.GridQubit(row=1, col=2)), complex)


def test_numpy_index():
np5, np6, np3 = [np.int64(i) for i in [5, 6, 3]]
@pytest.mark.parametrize('dtype', (np.int8, np.int64, float, np.float64))
def test_numpy_index(dtype):
np5, np6, np3 = [dtype(i) for i in [5, 6, 3]]
q = cirq.GridQubit(np5, np6)
hash(q) # doesn't throw
assert hash(q) == hash(cirq.GridQubit(5, 6))
assert q.row == 5
assert q.col == 6
assert q.dimension == 2
assert isinstance(q.row, int)
assert isinstance(q.col, int)
assert isinstance(q.dimension, int)

q = cirq.GridQid(np5, np6, dimension=np3)
hash(q) # doesn't throw
assert hash(q) == hash(cirq.GridQid(5, 6, dimension=3))
assert q.row == 5
assert q.col == 6
assert q.dimension == 3
assert isinstance(q.row, int)
assert isinstance(q.col, int)
assert isinstance(q.dimension, int)


@pytest.mark.parametrize('dtype', (float, np.float64))
def test_non_integer_index(dtype):
# Not supported type-wise, but is used in practice, so behavior needs to be preserved.
q = cirq.GridQubit(dtype(5.5), dtype(6.5))
assert hash(q) == hash(cirq.GridQubit(5.5, 6.5))
assert q.row == 5.5
assert q.col == 6.5
assert isinstance(q.row, dtype)
assert isinstance(q.col, dtype)
6 changes: 2 additions & 4 deletions cirq-core/cirq/devices/line_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def __new__(cls, x: int, dimension: int) -> 'cirq.LineQid':
dimension: The dimension of the qid's Hilbert space, i.e.
the number of quantum levels.
"""
x = int(x)
dimension = int(dimension)
key = (x, dimension)
inst = cls._cache.get(key)
Expand All @@ -200,7 +199,7 @@ def __new__(cls, x: int, dimension: int) -> 'cirq.LineQid':
inst = super().__new__(cls)
inst._x = x
inst._dimension = dimension
inst._hash = (dimension - 2) * 1_000_003 + x
inst._hash = (dimension - 2) * 1_000_003 + hash(x)
cls._cache[key] = inst
return inst

Expand Down Expand Up @@ -302,12 +301,11 @@ def __new__(cls, x: int) -> 'cirq.LineQubit':
Args:
x: The x coordinate.
"""
x = int(x)
inst = cls._cache.get(x)
if inst is None:
inst = super().__new__(cls)
inst._x = x
inst._hash = x
inst._hash = hash(x)
cls._cache[x] = inst
return inst

Expand Down
18 changes: 13 additions & 5 deletions cirq-core/cirq/devices/line_qubit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,26 @@ def test_numeric():
assert isinstance(complex(cirq.LineQubit(x=5)), complex)


def test_numpy_index():
np5 = np.int64(5)
@pytest.mark.parametrize('dtype', (np.int8, np.int64, float, np.float64))
def test_numpy_index(dtype):
np5 = dtype(5)
q = cirq.LineQubit(np5)
assert hash(q) == 5
assert q.x == 5
assert q.dimension == 2
assert isinstance(q.x, int)
assert isinstance(q.dimension, int)

q = cirq.LineQid(np5, np.int64(3))
q = cirq.LineQid(np5, dtype(3))
hash(q) # doesn't throw
assert q.x == 5
assert q.dimension == 3
assert isinstance(q.x, int)
assert isinstance(q.dimension, int)


@pytest.mark.parametrize('dtype', (float, np.float64))
def test_non_integer_index(dtype):
# Not supported type-wise, but is used in practice, so behavior needs to be preserved.
q = cirq.LineQubit(dtype(5.5))
assert q.x == 5.5
assert q.x == dtype(5.5)
assert isinstance(q.x, dtype)
2 changes: 2 additions & 0 deletions cirq-core/cirq/transformers/gauge_compiling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@
from cirq.transformers.gauge_compiling.sqrt_iswap_gauge import (
SqrtISWAPGaugeTransformer as SqrtISWAPGaugeTransformer,
)

from cirq.transformers.gauge_compiling.cphase_gauge import CPhaseGaugeTransformer
146 changes: 146 additions & 0 deletions cirq-core/cirq/transformers/gauge_compiling/cphase_gauge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Copyright 2025 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A Gauge Transformer for the cphase gate."""

import cirq.transformers.gauge_compiling.sqrt_cz_gauge as sqrt_cz_gauge

from cirq.transformers.gauge_compiling.gauge_compiling import (
GaugeTransformer,
GaugeSelector,
ConstantGauge,
Gauge,
TwoQubitGateSymbolizer,
)
from cirq import ops
import numpy as np


class CPhasePauliGauge(Gauge):
"""Gauges for the cphase gate (CZPowGate).
We identify 16 distinct gauges, corresponding to the 16 two-qubit Pauli operators that can be
inserted before the cphase gate. When an anticommuting gate is inserted, the cphase angle is
negated (or equivalently, the exponent of the CZPowGate is negated), so both postive and
negative angles should be calibrated to use this.
"""

def weight(self) -> float:
return 1.0

def _get_new_post(self, exponent: float, pre: ops.Gate) -> ops.Gate:
"""Identify the new single-qubit gate that needs to be inserted in the case that both pre
gates are X or Y.
Args:
exponent: The exponent of the CZPowGate that is getting transformed.
pre: The gate (X or Y) that is inserted on a given qubit.
Returns:
The single-qubit gate to insert after the CZPowGate on the same qubit.
Raises:
ValueError: If pre is not X or Y.
"""
if pre == ops.X:
return ops.PhasedXPowGate(exponent=1, phase_exponent=exponent / 2)
elif pre == ops.Y:
return ops.PhasedXZGate.from_zyz_exponents(z0=-exponent, y=1, z1=0)
else:
raise ValueError("pre should be cirq.X or cirq.Y") # pragma: no cover

def _get_constant_gauge(
self, gate: ops.CZPowGate, pre_q0: ops.Gate, pre_q1: ops.Gate
) -> ConstantGauge:
"""Get the ConstantGauge corresponding to a given pre_q0 and pre_q1.
Args:
gate: The particular cphase gate to transform.
pre_q0: The Pauli (I, X, Y, or Z) to insert before the cphase gate on q0.
pre_q1: The Pauli (I, X, Y, or Z) to insert before the cphase gate on q1.
Returns:
The ConstantGauge implementing the given transformation.
Raises:
ValueError: If pre_q0 and pre_q1 are not both in {I, X, Y, Z}.
"""

exponent = gate.exponent
commuting_paulis = {ops.I, ops.Z}
anticommuting_paulis = {ops.X, ops.Y}
if pre_q0 in commuting_paulis and pre_q1 in commuting_paulis:
return ConstantGauge(
two_qubit_gate=gate, pre_q0=pre_q0, pre_q1=pre_q1, post_q0=pre_q0, post_q1=pre_q1
)
elif pre_q0 in anticommuting_paulis and pre_q1 in commuting_paulis:
new_gate = ops.CZ ** (-exponent)
post_q1 = ops.Z ** (exponent) if pre_q1 == ops.I else ops.Z ** (1 + exponent)
return ConstantGauge(
two_qubit_gate=new_gate,
pre_q0=pre_q0,
pre_q1=pre_q1,
post_q0=pre_q0,
post_q1=post_q1,
)
elif pre_q0 in commuting_paulis and pre_q1 in anticommuting_paulis:
new_gate = ops.CZ ** (-exponent)
post_q0 = ops.Z ** (exponent) if pre_q0 == ops.I else ops.Z ** (1 + exponent)
return ConstantGauge(
two_qubit_gate=new_gate,
pre_q0=pre_q0,
pre_q1=pre_q1,
post_q0=post_q0,
post_q1=pre_q1,
)
elif pre_q0 in anticommuting_paulis and pre_q1 in anticommuting_paulis:
return ConstantGauge(
two_qubit_gate=gate,
pre_q0=pre_q0,
pre_q1=pre_q1,
post_q0=self._get_new_post(exponent, pre_q0),
post_q1=self._get_new_post(exponent, pre_q1),
)
else:
raise ValueError("pre_q0 and pre_q1 should be X, Y, Z, or I") # pragma: no cover

def sample(self, gate: ops.Gate, prng: np.random.Generator) -> ConstantGauge:
"""Sample the 16 cphase gauges at random.
Args:
gate: The CZPowGate to transform.
prng: The pseudorandom number generator.
Returns:
A ConstantGauge implementing the transformation.
Raises:
TypeError: if gate is not a CZPowGate
"""

if not type(gate) == ops.CZPowGate:
raise TypeError("gate must be a CZPowGate") # pragma: no cover
pre_q0, pre_q1 = prng.choice(np.array([ops.I, ops.X, ops.Y, ops.Z]), size=2, replace=True)
return self._get_constant_gauge(gate, pre_q0, pre_q1)


CPhaseGaugeSelector = GaugeSelector(gauges=[CPhasePauliGauge()])

CPhaseGaugeTransformer = GaugeTransformer(
target=ops.Gateset(ops.CZPowGate),
gauge_selector=CPhaseGaugeSelector,
two_qubit_gate_symbolizer=TwoQubitGateSymbolizer(
symbolizer_fn=sqrt_cz_gauge._symbolize_as_cz_pow, n_symbols=1
),
)
47 changes: 47 additions & 0 deletions cirq-core/cirq/transformers/gauge_compiling/cphase_gauge_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2025 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import cirq
from cirq.transformers.gauge_compiling.cphase_gauge import CPhaseGaugeTransformer
from cirq.transformers.gauge_compiling.gauge_compiling_test_utils import GaugeTester


class TestCPhaseGauge_0_3(GaugeTester):
two_qubit_gate = cirq.CZ**0.3
gauge_transformer = CPhaseGaugeTransformer
sweep_must_pass = True


class TestCPhaseGauge_m0_3(GaugeTester):
two_qubit_gate = cirq.CZ ** (-0.3)
gauge_transformer = CPhaseGaugeTransformer
sweep_must_pass = True


class TestCPhaseGauge_0_1(GaugeTester):
two_qubit_gate = cirq.CZ**0.1
gauge_transformer = CPhaseGaugeTransformer
sweep_must_pass = True


class TestCPhaseGauge_m0_1(GaugeTester):
two_qubit_gate = cirq.CZ ** (-0.1)
gauge_transformer = CPhaseGaugeTransformer
sweep_must_pass = True


class TestCPhaseGauge_0_7(GaugeTester):
two_qubit_gate = cirq.CZ**0.7
gauge_transformer = CPhaseGaugeTransformer
sweep_must_pass = True
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@
}
GATE_PREFIX_PAIRS: Dict[Type['cirq.Gate'], str] = {
cg_ops.SycamoreGate: 'two_qubit_parallel_sycamore_gate',
ops.CZPowGate: 'two_qubit_parallel_cz_gate',
ops.ISwapPowGate: 'two_qubit_parallel_sqrt_iswap_gate',
}
GATE_ZPHASE_CODE_PAIRS: Dict[Type['cirq.Gate'], str] = {
cg_ops.SycamoreGate: 'syc',
ops.CZPowGate: 'cz',
ops.ISwapPowGate: 'sqrt_iswap',
}

Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Here is a summary of the examples found in this directory:
by Harrow, Hassidim, and Lloyd.
* [`hidden_shift_algorithm.py`](hidden_shift_algorithm.py):
demonstration of a Hidden Shift algorithm.
* [`magic_square.py`](magic_square.py):
demonstration of the Mermin-Peres magic square game.
* [`noisy_simulation_example.py`](noisy_simulation_example.py): example
of a noisy circuit using the `cirq.ConstantQubitNoiseModel` class.
* [`phase_estimator.py`](phase_estimator.py): example of a phase
Expand Down
Loading

0 comments on commit bda4094

Please sign in to comment.