Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions cirq-google/cirq_google/experimental/ops/coupler_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Optional, Tuple
from typing import AbstractSet, Any, Optional, Tuple

import numpy as np

import cirq
from cirq._compat import proper_repr


@cirq.value_equality(approximate=True)
Expand Down Expand Up @@ -51,7 +52,7 @@ class CouplerPulse(cirq.ops.Gate):
def __init__(
self,
hold_time: cirq.Duration,
coupling_mhz: float,
coupling_mhz: cirq.TParamVal,
rise_time: Optional[cirq.Duration] = cirq.Duration(nanos=8),
padding_time: Optional[cirq.Duration] = cirq.Duration(nanos=2.5),
):
Expand All @@ -78,10 +79,10 @@ def _unitary_(self) -> np.ndarray:
def __repr__(self) -> str:
return (
'cirq_google.experimental.ops.coupler_pulse.'
+ f'CouplerPulse(hold_time={self.hold_time!r}, '
+ f'coupling_mhz={self.coupling_mhz}, '
+ f'rise_time={self.rise_time!r}, '
+ f'padding_time={self.padding_time!r})'
+ f'CouplerPulse(hold_time={proper_repr(self.hold_time)}, '
+ f'coupling_mhz={proper_repr(self.coupling_mhz)}, '
+ f'rise_time={proper_repr(self.rise_time)}, '
+ f'padding_time={proper_repr(self.padding_time)})'
)

def __str__(self) -> str:
Expand All @@ -92,6 +93,32 @@ def __str__(self) -> str:
+ f'padding_time={self.padding_time})'
)

def _is_parameterized_(self) -> bool:
return (
cirq.is_parameterized(self.hold_time)
or cirq.is_parameterized(self.coupling_mhz)
or cirq.is_parameterized(self.rise_time)
or cirq.is_parameterized(self.padding_time)
)

def _parameter_names_(self: Any) -> AbstractSet[str]:
return (
cirq.parameter_names(self.hold_time)
| cirq.parameter_names(self.coupling_mhz)
| cirq.parameter_names(self.rise_time)
| cirq.parameter_names(self.padding_time)
)

def _resolve_parameters_(
self, resolver: cirq.ParamResolverOrSimilarType, recursive: bool
) -> 'CouplerPulse':
return CouplerPulse(
hold_time=cirq.resolve_parameters(self.hold_time, resolver, recursive=recursive),
coupling_mhz=cirq.resolve_parameters(self.coupling_mhz, resolver, recursive=recursive),
rise_time=cirq.resolve_parameters(self.rise_time, resolver, recursive=recursive),
padding_time=cirq.resolve_parameters(self.padding_time, resolver, recursive=recursive),
)

def _value_equality_values_(self) -> Any:
return self.hold_time, self.coupling_mhz, self.rise_time, self.padding_time

Expand Down
80 changes: 80 additions & 0 deletions cirq-google/cirq_google/experimental/ops/coupler_pulse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# 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 pytest
import sympy

import cirq
import cirq_google.experimental.ops.coupler_pulse as coupler_pulse
Expand Down Expand Up @@ -112,3 +114,81 @@ def test_coupler_pulse_circuit_diagram():
1: ───/‾‾(10 ns@25.0MHz)‾‾\───
""",
)


@pytest.mark.parametrize(
'gate, resolver, expected',
[
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
{'t_ns': 50},
coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10),
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
{'g': 10},
coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10),
),
],
)
def test_coupler_pulse_resolution(gate, resolver, expected):
assert cirq.resolve_parameters(gate, resolver) == expected


@pytest.mark.parametrize(
'gate, param_names',
[
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
{'t_ns'},
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
{'g'},
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=sympy.Symbol('g')
),
{'g', 't_ns'},
),
],
)
def test_coupler_pulse_parameter_names(gate, param_names):
assert cirq.parameter_names(gate) == param_names


@pytest.mark.parametrize(
'gate, is_parameterized',
[
(coupler_pulse.CouplerPulse(hold_time=cirq.Duration(nanos=50), coupling_mhz=10), False),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=10
),
True,
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=50), coupling_mhz=sympy.Symbol('g')
),
True,
),
(
coupler_pulse.CouplerPulse(
hold_time=cirq.Duration(nanos=sympy.Symbol('t_ns')), coupling_mhz=sympy.Symbol('g')
),
True,
),
],
)
def test_coupler_pulse_is_parameterized(gate, is_parameterized):
assert cirq.is_parameterized(gate) == is_parameterized