Skip to content

Commit

Permalink
Format with black skip-magic-trailing-comma
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Mar 31, 2022
1 parent f82b6fd commit b76b7a2
Show file tree
Hide file tree
Showing 83 changed files with 409 additions and 1,492 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
6 changes: 1 addition & 5 deletions cirq-google/cirq_google/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
# limitations under the License.
"""Data format v1 for google api."""

from cirq_google.api.v1 import (
operations_pb2,
params_pb2,
program_pb2,
)
from cirq_google.api.v1 import operations_pb2, params_pb2, program_pb2

from cirq_google.api.v1.params import sweep_from_proto, sweep_to_proto

Expand Down
4 changes: 1 addition & 3 deletions cirq-google/cirq_google/api/v1/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def _sweep_from_param_sweep_zip_proto(param_sweep_zip: params_pb2.ZipSweep) -> c
return cirq.UnitSweep


def _sweep_from_single_param_sweep_proto(
single_param_sweep: params_pb2.SingleSweep,
) -> cirq.Sweep:
def _sweep_from_single_param_sweep_proto(single_param_sweep: params_pb2.SingleSweep) -> cirq.Sweep:
key = single_param_sweep.parameter_key
if single_param_sweep.HasField('points'):
points = single_param_sweep.points
Expand Down
3 changes: 1 addition & 2 deletions cirq-google/cirq_google/api/v1/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ def xmon_op_from_proto(proto: operations_pb2.Operation) -> cirq.Operation:
if proto.HasField('exp_w'):
exp_w = proto.exp_w
return cirq.PhasedXPowGate(
exponent=param(exp_w.half_turns),
phase_exponent=param(exp_w.axis_half_turns),
exponent=param(exp_w.half_turns), phase_exponent=param(exp_w.axis_half_turns)
).on(qubit(exp_w.target))
if proto.HasField('exp_z'):
exp_z = proto.exp_z
Expand Down
69 changes: 14 additions & 55 deletions cirq-google/cirq_google/api/v1/programs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,9 @@ def test_pack_results():
measurements = [
(
'a',
np.array(
[
[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
]
),
),
(
'b',
np.array(
[
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[0, 0],
[0, 1],
[1, 0],
]
),
np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]),
),
('b', np.array([[0, 0], [0, 1], [1, 0], [1, 1], [0, 0], [0, 1], [1, 0]])),
]
data = programs.pack_results(measurements)
expected = make_bytes(
Expand Down Expand Up @@ -168,32 +145,14 @@ def test_unpack_results():
assert results['a'].shape == (7, 3)
assert results['a'].dtype == bool
np.testing.assert_array_equal(
results['a'],
[
[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
],
results['a'], [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]
)

assert 'b' in results
assert results['b'].shape == (7, 2)
assert results['b'].dtype == bool
np.testing.assert_array_equal(
results['b'],
[
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[0, 0],
[0, 1],
[1, 0],
],
results['b'], [[0, 0], [0, 1], [1, 0], [1, 1], [0, 0], [0, 1], [1, 0]]
)


Expand Down Expand Up @@ -252,7 +211,7 @@ def test_z_proto_convert():
)

assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))
gate = cirq.Z ** 0.5
gate = cirq.Z**0.5
proto = operations_pb2.Operation(
exp_z=operations_pb2.ExpZ(
target=operations_pb2.Qubit(row=2, col=3),
Expand All @@ -273,7 +232,7 @@ def test_cz_proto_convert():
)
assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3), cirq.GridQubit(3, 4))

gate = cirq.CZ ** 0.5
gate = cirq.CZ**0.5
proto = operations_pb2.Operation(
exp_11=operations_pb2.Exp11(
target1=operations_pb2.Qubit(row=2, col=3),
Expand Down Expand Up @@ -305,7 +264,7 @@ def test_w_to_proto():
)
assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

gate = cirq.X ** 0.25
gate = cirq.X**0.25
proto = operations_pb2.Operation(
exp_w=operations_pb2.ExpW(
target=operations_pb2.Qubit(row=2, col=3),
Expand All @@ -315,7 +274,7 @@ def test_w_to_proto():
)
assert_proto_dict_convert(gate, proto, cirq.GridQubit(2, 3))

gate = cirq.Y ** 0.25
gate = cirq.Y**0.25
proto = operations_pb2.Operation(
exp_w=operations_pb2.ExpW(
target=operations_pb2.Qubit(row=2, col=3),
Expand Down Expand Up @@ -347,9 +306,9 @@ def test_unsupported_op():

def test_invalid_to_proto_dict_qubit_number():
with pytest.raises(ValueError, match='Wrong number of qubits'):
_ = programs.gate_to_proto(cirq.CZ ** 0.5, (cirq.GridQubit(2, 3),), delay=0)
_ = programs.gate_to_proto(cirq.CZ**0.5, (cirq.GridQubit(2, 3),), delay=0)
with pytest.raises(ValueError, match='Wrong number of qubits'):
programs.gate_to_proto(cirq.Z ** 0.5, (cirq.GridQubit(2, 3), cirq.GridQubit(3, 4)), delay=0)
programs.gate_to_proto(cirq.Z**0.5, (cirq.GridQubit(2, 3), cirq.GridQubit(3, 4)), delay=0)
with pytest.raises(ValueError, match='Wrong number of qubits'):
programs.gate_to_proto(
cirq.PhasedXPowGate(exponent=0.5, phase_exponent=0),
Expand Down Expand Up @@ -398,10 +357,10 @@ def test_is_supported():

def test_is_native_xmon_gate():
assert programs.is_native_xmon_gate(cirq.CZ)
assert programs.is_native_xmon_gate(cirq.X ** 0.5)
assert programs.is_native_xmon_gate(cirq.Y ** 0.5)
assert programs.is_native_xmon_gate(cirq.Z ** 0.5)
assert programs.is_native_xmon_gate(cirq.X**0.5)
assert programs.is_native_xmon_gate(cirq.Y**0.5)
assert programs.is_native_xmon_gate(cirq.Z**0.5)
assert programs.is_native_xmon_gate(cirq.PhasedXPowGate(phase_exponent=0.2) ** 0.5)
assert programs.is_native_xmon_gate(cirq.Z ** 1)
assert programs.is_native_xmon_gate(cirq.Z**1)
assert not programs.is_native_xmon_gate(cirq.CCZ)
assert not programs.is_native_xmon_gate(cirq.SWAP)
21 changes: 4 additions & 17 deletions cirq-google/cirq_google/api/v2/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
# 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.
from typing import (
cast,
Dict,
Hashable,
Iterable,
Iterator,
List,
Optional,
Sequence,
Set,
)
from typing import cast, Dict, Hashable, Iterable, Iterator, List, Optional, Sequence, Set
from collections import OrderedDict
import dataclasses
import numpy as np
Expand Down Expand Up @@ -161,8 +151,7 @@ def results_to_proto(


def results_from_proto(
msg: result_pb2.Result,
measurements: List[MeasureInfo] = None,
msg: result_pb2.Result, measurements: List[MeasureInfo] = None
) -> Sequence[Sequence[cirq.Result]]:
"""Converts a v2 result proto into List of list of trial results.
Expand All @@ -184,8 +173,7 @@ def results_from_proto(


def _trial_sweep_from_proto(
msg: result_pb2.SweepResult,
measure_map: Dict[str, MeasureInfo] = None,
msg: result_pb2.SweepResult, measure_map: Dict[str, MeasureInfo] = None
) -> Sequence[cirq.Result]:
"""Converts a SweepResult proto into List of list of trial results.
Expand Down Expand Up @@ -220,8 +208,7 @@ def _trial_sweep_from_proto(
m_data[mr.key] = np.array(ordered_results).transpose()
trial_sweep.append(
cirq.ResultDict(
params=cirq.ParamResolver(dict(pr.params.assignments)),
measurements=m_data,
params=cirq.ParamResolver(dict(pr.params.assignments)), measurements=m_data
)
)
return trial_sweep
42 changes: 9 additions & 33 deletions cirq-google/cirq_google/api/v2/results_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,21 @@ def test_results_to_proto():
[
cirq.ResultDict(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0], [1], [0], [1]], dtype=bool),
},
measurements={'foo': np.array([[0], [1], [0], [1]], dtype=bool)},
),
cirq.ResultDict(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1], [1], [0]], dtype=bool),
},
measurements={'foo': np.array([[0], [1], [1], [0]], dtype=bool)},
),
],
[
cirq.ResultDict(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0], [1], [0], [1]], dtype=bool),
},
measurements={'foo': np.array([[0], [1], [0], [1]], dtype=bool)},
),
cirq.ResultDict(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1], [1], [0]], dtype=bool),
},
measurements={'foo': np.array([[0], [1], [1], [0]], dtype=bool)},
),
],
]
Expand All @@ -189,15 +181,11 @@ def test_results_to_proto_sweep_repetitions():
[
cirq.ResultDict(
params=cirq.ParamResolver({'i': 0}),
measurements={
'foo': np.array([[0]], dtype=bool),
},
measurements={'foo': np.array([[0]], dtype=bool)},
),
cirq.ResultDict(
params=cirq.ParamResolver({'i': 1}),
measurements={
'foo': np.array([[0], [1]], dtype=bool),
},
measurements={'foo': np.array([[0], [1]], dtype=bool)},
),
]
]
Expand All @@ -218,11 +206,7 @@ def test_results_from_proto_qubit_ordering():
pr.params.assignments.update({'i': 1})
mr = pr.measurement_results.add()
mr.key = 'foo'
for qubit, results in [
(q(0, 1), 0b1100_1100),
(q(1, 1), 0b1010_1010),
(q(0, 0), 0b1111_0000),
]:
for qubit, results in [(q(0, 1), 0b1100_1100), (q(1, 1), 0b1010_1010), (q(0, 0), 0b1111_0000)]:
qmr = mr.qubit_measurement_results.add()
qmr.qubit.id = v2.qubit_to_proto_id(qubit)
qmr.results = bytes([results])
Expand Down Expand Up @@ -262,11 +246,7 @@ def test_results_from_proto_duplicate_qubit():
pr.params.assignments.update({'i': 0})
mr = pr.measurement_results.add()
mr.key = 'foo'
for qubit, results in [
(q(0, 0), 0b1100_1100),
(q(0, 1), 0b1010_1010),
(q(0, 1), 0b1111_0000),
]:
for qubit, results in [(q(0, 0), 0b1100_1100), (q(0, 1), 0b1010_1010), (q(0, 1), 0b1111_0000)]:
qmr = mr.qubit_measurement_results.add()
qmr.qubit.id = v2.qubit_to_proto_id(qubit)
qmr.results = bytes([results])
Expand All @@ -282,11 +262,7 @@ def test_results_from_proto_default_ordering():
pr.params.assignments.update({'i': 1})
mr = pr.measurement_results.add()
mr.key = 'foo'
for qubit, results in [
(q(0, 1), 0b1100_1100),
(q(1, 1), 0b1010_1010),
(q(0, 0), 0b1111_0000),
]:
for qubit, results in [(q(0, 1), 0b1100_1100), (q(1, 1), 0b1010_1010), (q(0, 0), 0b1111_0000)]:
qmr = mr.qubit_measurement_results.add()
qmr.qubit.id = v2.qubit_to_proto_id(qubit)
qmr.results = bytes([results])
Expand Down
4 changes: 1 addition & 3 deletions cirq-google/cirq_google/api/v2/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@


def sweep_to_proto(
sweep: cirq.Sweep,
*,
out: Optional[run_context_pb2.Sweep] = None,
sweep: cirq.Sweep, *, out: Optional[run_context_pb2.Sweep] = None
) -> run_context_pb2.Sweep:
"""Converts a Sweep to v2 protobuf message.
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
Loading

0 comments on commit b76b7a2

Please sign in to comment.