Skip to content

Commit

Permalink
Format cirq-google with skip-magic-trailing-comma (quantumlib#5171)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo authored and rht committed May 1, 2023
1 parent 54d1b6b commit a959527
Show file tree
Hide file tree
Showing 66 changed files with 309 additions and 1,115 deletions.
12 changes: 2 additions & 10 deletions cirq-google/cirq_google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
from cirq import _compat
from cirq_google import api

from cirq_google._version import (
__version__,
)
from cirq_google._version import __version__

from cirq_google.calibration import (
ALL_ANGLES_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
Expand Down Expand Up @@ -89,13 +87,7 @@
LinePlacementStrategy,
)

from cirq_google.ops import (
CalibrationTag,
FSimGateFamily,
PhysicalZTag,
SycamoreGate,
SYC,
)
from cirq_google.ops import CalibrationTag, FSimGateFamily, PhysicalZTag, SycamoreGate, SYC

from cirq_google.optimizers import (
ConvertToXmonGates,
Expand Down
4 changes: 1 addition & 3 deletions cirq-google/cirq_google/calibration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from cirq_google.calibration.engine_simulator import (
PhasedFSimEngineSimulator,
)
from cirq_google.calibration.engine_simulator import PhasedFSimEngineSimulator

from cirq_google.calibration.phased_fsim import (
ALL_ANGLES_FLOQUET_PHASED_FSIM_CHARACTERIZATION,
Expand Down
27 changes: 4 additions & 23 deletions cirq-google/cirq_google/calibration/engine_simulator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
# pylint: disable=wrong-or-nonexistent-copyright-notice
from typing import (
Any,
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Sequence,
Tuple,
Union,
)
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -75,9 +64,7 @@ def __init__(

@classmethod
def create_with_ideal_sqrt_iswap(
cls,
*,
simulator: Optional[cirq.Simulator] = None,
cls, *, simulator: Optional[cirq.Simulator] = None
) -> 'PhasedFSimEngineSimulator':
"""Creates a PhasedFSimEngineSimulator that simulates ideal FSimGate(theta=π/4, phi=0).
Expand Down Expand Up @@ -452,10 +439,7 @@ def create_gate_with_drift(
return gate_calibration.as_characterized_phased_fsim_gate(parameters)

def run_sweep_iter(
self,
program: cirq.AbstractCircuit,
params: cirq.Sweepable,
repetitions: int = 1,
self, program: cirq.AbstractCircuit, params: cirq.Sweepable, repetitions: int = 1
) -> Iterator[cirq.Result]:
converted = _convert_to_circuit_with_drift(self, program)
yield from self._simulator.run_sweep_iter(converted, params, repetitions)
Expand All @@ -479,10 +463,7 @@ def _create_partial_act_on_args(
# Needs an implementation since it's abstract but will never actually be called.
raise NotImplementedError()

def _create_step_result(
self,
sim_state: cirq.OperationTarget,
) -> cirq.SparseSimulatorStep:
def _create_step_result(self, sim_state: cirq.OperationTarget) -> cirq.SparseSimulatorStep:
# Needs an implementation since it's abstract but will never actually be called.
raise NotImplementedError()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_from_dictionary_sqrt_iswap_ideal_when_missing_parameter_fails():
circuit = cirq.Circuit(cirq.FSimGate(np.pi / 4, 0.0).on(a, b))

engine_simulator = PhasedFSimEngineSimulator.create_from_dictionary_sqrt_iswap(
parameters={(a, b): parameters_ab},
parameters={(a, b): parameters_ab}
)

with pytest.raises(ValueError):
Expand Down
39 changes: 10 additions & 29 deletions cirq-google/cirq_google/calibration/phased_fsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
import pandas as pd

import cirq
from cirq.experiments.xeb_fitting import (
XEBPhasedFSimCharacterizationOptions,
)
from cirq.experiments.xeb_fitting import XEBPhasedFSimCharacterizationOptions
from cirq_google.api import v2
from cirq_google.engine import Calibration, CalibrationLayer, CalibrationResult, Engine, EngineJob
from cirq_google.ops import FSimGateFamily
Expand Down Expand Up @@ -211,9 +209,7 @@ class PhasedFSimCalibrationOptions(abc.ABC, Generic[RequestT]):

@abc.abstractmethod
def create_phased_fsim_request(
self,
pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...],
gate: cirq.Gate,
self, pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...], gate: cirq.Gate
) -> RequestT:
"""Create a PhasedFSimCalibrationRequest of the correct type for these options.
Expand Down Expand Up @@ -304,8 +300,7 @@ def engine_calibration(self) -> Optional[Calibration]:

@classmethod
def _create_parameters_dict(
cls,
parameters: List[Tuple[cirq.Qid, cirq.Qid, PhasedFSimCharacterization]],
cls, parameters: List[Tuple[cirq.Qid, cirq.Qid, PhasedFSimCharacterization]]
) -> Dict[Tuple[cirq.Qid, cirq.Qid], PhasedFSimCharacterization]:
"""Utility function to create parameters from JSON.
Expand All @@ -314,10 +309,7 @@ def _create_parameters_dict(
return {(q_a, q_b): params for q_a, q_b, params in parameters}

@classmethod
def _from_json_dict_(
cls,
**kwargs,
) -> 'PhasedFSimCalibrationResult':
def _from_json_dict_(cls, **kwargs) -> 'PhasedFSimCalibrationResult':
"""Magic method for the JSON serialization protocol.
Converts serialized dictionary into a dict suitable for
Expand Down Expand Up @@ -482,9 +474,7 @@ def to_args(self) -> Dict[str, Any]:
return args

def create_phased_fsim_request(
self,
pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...],
gate: cirq.Gate,
self, pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...], gate: cirq.Gate
) -> 'XEBPhasedFSimCalibrationRequest':
return XEBPhasedFSimCalibrationRequest(pairs=pairs, gate=gate, options=self)

Expand Down Expand Up @@ -537,9 +527,7 @@ class LocalXEBPhasedFSimCalibrationOptions(XEBPhasedFSimCalibrationOptions):
n_processes: Optional[int] = None

def create_phased_fsim_request(
self,
pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...],
gate: cirq.Gate,
self, pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...], gate: cirq.Gate
):
return LocalXEBPhasedFSimCalibrationRequest(pairs=pairs, gate=gate, options=self)

Expand Down Expand Up @@ -588,9 +576,7 @@ def zeta_chi_gamma_correction_override(self) -> PhasedFSimCharacterization:
)

def create_phased_fsim_request(
self,
pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...],
gate: cirq.Gate,
self, pairs: Tuple[Tuple[cirq.Qid, cirq.Qid], ...], gate: cirq.Gate
) -> 'FloquetPhasedFSimCalibrationRequest':
return FloquetPhasedFSimCalibrationRequest(pairs=pairs, gate=gate, options=self)

Expand Down Expand Up @@ -693,9 +679,7 @@ def to_calibration_layer(self) -> CalibrationLayer:
self.options.readout_error_tolerance
)
return CalibrationLayer(
calibration_type=_FLOQUET_PHASED_FSIM_HANDLER_NAME,
program=circuit,
args=args,
calibration_type=_FLOQUET_PHASED_FSIM_HANDLER_NAME, program=circuit, args=args
)

def parse_result(
Expand Down Expand Up @@ -803,8 +787,7 @@ def _parse_xeb_fidelities_df(metrics: 'cirq_google.Calibration', super_name: str


def _parse_characterized_angles(
metrics: 'cirq_google.Calibration',
super_name: str,
metrics: 'cirq_google.Calibration', super_name: str
) -> Dict[Tuple[cirq.Qid, cirq.Qid], Dict[str, float]]:
"""Parses characterized angles from Metric protos.
Expand Down Expand Up @@ -1088,9 +1071,7 @@ def try_convert_gate_to_fsim(gate: cirq.Gate) -> Optional[PhaseCalibratedFSimGat
return PhaseCalibratedFSimGate(cirq.FSimGate(theta=theta, phi=phi), phase_exponent)


def try_convert_syc_or_sqrt_iswap_to_fsim(
gate: cirq.Gate,
) -> Optional[PhaseCalibratedFSimGate]:
def try_convert_syc_or_sqrt_iswap_to_fsim(gate: cirq.Gate) -> Optional[PhaseCalibratedFSimGate]:
"""Converts a gate to equivalent PhaseCalibratedFSimGate if possible.
Args:
Expand Down
88 changes: 18 additions & 70 deletions cirq-google/cirq_google/calibration/phased_fsim_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ def test_floquet_to_calibration_layer_with_measure_qubits():

assert request.to_calibration_layer() == cirq_google.CalibrationLayer(
calibration_type='floquet_phased_fsim_characterization',
program=cirq.Circuit(
[gate.on(q_00, q_01), gate.on(q_02, q_03), cirq.measure(*qubits)],
),
program=cirq.Circuit([gate.on(q_00, q_01), gate.on(q_02, q_03), cirq.measure(*qubits)]),
args={
'est_theta': True,
'est_zeta': True,
Expand Down Expand Up @@ -459,7 +457,7 @@ def test_xeb_parse_bad_fidelities():
metrics = cirq_google.Calibration(
metrics={
'initial_fidelities_depth_5': {
('layer_0', 'pair_0', cirq.GridQubit(0, 0), cirq.GridQubit(1, 1)): [1.0],
('layer_0', 'pair_0', cirq.GridQubit(0, 0), cirq.GridQubit(1, 1)): [1.0]
}
}
)
Expand All @@ -478,21 +476,13 @@ def test_xeb_parse_bad_fidelities():
)

metrics = cirq_google.Calibration(
metrics={
'initial_fidelities_depth_5x': {
('layer_0', 'pair_0', '0_0', '1_1'): [1.0],
}
}
metrics={'initial_fidelities_depth_5x': {('layer_0', 'pair_0', '0_0', '1_1'): [1.0]}}
)
df = _parse_xeb_fidelities_df(metrics, 'initial_fidelities')
assert len(df) == 0, 'bad metric name ignored'

metrics = cirq_google.Calibration(
metrics={
'initial_fidelities_depth_5': {
('bad_name_0', 'pair_0', '0_0', '1_1'): [1.0],
}
}
metrics={'initial_fidelities_depth_5': {('bad_name_0', 'pair_0', '0_0', '1_1'): [1.0]}}
)
with pytest.raises(ValueError, match=r'Could not parse layer value for bad_name_0'):
_parse_xeb_fidelities_df(metrics, 'initial_fidelities')
Expand Down Expand Up @@ -589,12 +579,8 @@ def test_floquet_parse_result_bad_metric():
metrics=[
cirq_google.api.v2.metrics_pb2.Metric(
name='angles',
targets=[
'1000gerbils',
],
values=[
cirq_google.api.v2.metrics_pb2.Value(str_val='100_10'),
],
targets=['1000gerbils'],
values=[cirq_google.api.v2.metrics_pb2.Value(str_val='100_10')],
)
]
)
Expand Down Expand Up @@ -642,33 +628,19 @@ def test_merge_matching_results():
gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0)
options = WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION
parameters_1 = {
(q_00, q_01): PhasedFSimCharacterization(
theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3
),
(q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3)
}
parameters_2 = {
(q_02, q_03): PhasedFSimCharacterization(
theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6
),
(q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6)
}

results = [
PhasedFSimCalibrationResult(
parameters=parameters_1,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(
parameters=parameters_2,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(parameters=parameters_1, gate=gate, options=options),
PhasedFSimCalibrationResult(parameters=parameters_2, gate=gate, options=options),
]

assert merge_matching_results(results) == PhasedFSimCalibrationResult(
parameters={**parameters_1, **parameters_2},
gate=gate,
options=options,
parameters={**parameters_1, **parameters_2}, gate=gate, options=options
)


Expand All @@ -681,53 +653,29 @@ def test_merge_matching_results_when_incompatible_fails():
gate = cirq.FSimGate(theta=np.pi / 4, phi=0.0)
options = WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION
parameters_1 = {
(q_00, q_01): PhasedFSimCharacterization(
theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3
),
(q_00, q_01): PhasedFSimCharacterization(theta=0.1, zeta=0.2, chi=None, gamma=None, phi=0.3)
}
parameters_2 = {
(q_02, q_03): PhasedFSimCharacterization(
theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6
),
(q_02, q_03): PhasedFSimCharacterization(theta=0.4, zeta=0.5, chi=None, gamma=None, phi=0.6)
}

with pytest.raises(ValueError):
results = [
PhasedFSimCalibrationResult(
parameters=parameters_1,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(
parameters=parameters_1,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(parameters=parameters_1, gate=gate, options=options),
PhasedFSimCalibrationResult(parameters=parameters_1, gate=gate, options=options),
]
assert merge_matching_results(results)

with pytest.raises(ValueError):
results = [
PhasedFSimCalibrationResult(
parameters=parameters_1,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(
parameters=parameters_2,
gate=cirq.CZ,
options=options,
),
PhasedFSimCalibrationResult(parameters=parameters_1, gate=gate, options=options),
PhasedFSimCalibrationResult(parameters=parameters_2, gate=cirq.CZ, options=options),
]
assert merge_matching_results(results)

with pytest.raises(ValueError):
results = [
PhasedFSimCalibrationResult(
parameters=parameters_1,
gate=gate,
options=options,
),
PhasedFSimCalibrationResult(parameters=parameters_1, gate=gate, options=options),
PhasedFSimCalibrationResult(
parameters=parameters_2,
gate=gate,
Expand Down
Loading

0 comments on commit a959527

Please sign in to comment.