Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format cirq-core with latest version of black #5159

Merged
merged 1 commit into from
Mar 29, 2022
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
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def _superoperator_(self) -> np.ndarray:
if n > 10:
raise ValueError(f"{n} > 10 qubits is too many to compute superoperator")

circuit_superoperator = np.eye(4 ** n)
circuit_superoperator = np.eye(4**n)
for moment in self:
full_moment = moment.expand_to(all_qubits)
moment_superoperator = full_moment._superoperator_()
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/circuit_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def _mapped_single_loop(self, repetition_id: Optional[str] = None) -> 'cirq.Circ
if self.qubit_map:
circuit = circuit.transform_qubits(lambda q: self.qubit_map.get(q, q))
if isinstance(self.repetitions, INT_CLASSES) and self.repetitions < 0:
circuit = circuit ** -1
circuit = circuit**-1
if self.measurement_key_map:
circuit = protocols.with_measurement_key_mapping(circuit, self.measurement_key_map)
if self.param_resolver:
Expand Down
14 changes: 7 additions & 7 deletions cirq-core/cirq/circuits/circuit_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ def test_repeat(add_measurements, use_default_ids_for_initial_rep):
if use_default_ids_for_initial_rep:
op_with_reps = op_base.repeat(initial_repetitions)
rep_ids = ['0', '1', '2']
assert op_base ** initial_repetitions == op_with_reps
assert op_base**initial_repetitions == op_with_reps
else:
rep_ids = ['a', 'b', 'c']
op_with_reps = op_base.repeat(initial_repetitions, rep_ids)
assert op_base ** initial_repetitions != op_with_reps
assert (op_base ** initial_repetitions).replace(repetition_ids=rep_ids) == op_with_reps
assert op_base**initial_repetitions != op_with_reps
assert (op_base**initial_repetitions).replace(repetition_ids=rep_ids) == op_with_reps
assert op_with_reps.repetitions == initial_repetitions
assert op_with_reps.repetition_ids == rep_ids
assert op_with_reps.repeat(1) is op_with_reps
Expand All @@ -322,7 +322,7 @@ def test_repeat(add_measurements, use_default_ids_for_initial_rep):
op_with_consecutive_reps = op_with_reps.repeat(2)
assert op_with_consecutive_reps.repetitions == final_repetitions
assert op_with_consecutive_reps.repetition_ids == _full_join_string_lists(['0', '1'], rep_ids)
assert op_base ** final_repetitions != op_with_consecutive_reps
assert op_base**final_repetitions != op_with_consecutive_reps

op_with_consecutive_reps = op_with_reps.repeat(2, ['a', 'b'])
assert op_with_reps.repeat(repetition_ids=['a', 'b']) == op_with_consecutive_reps
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_repeat_zero_times(add_measurements, use_repetition_ids, initial_reps):
)
result = cirq.Simulator().simulate(cirq.Circuit(op))
assert np.allclose(result.state_vector(), [0, 1] if initial_reps % 2 else [1, 0])
result = cirq.Simulator().simulate(cirq.Circuit(op ** 0))
result = cirq.Simulator().simulate(cirq.Circuit(op**0))
assert np.allclose(result.state_vector(), [1, 0])


Expand All @@ -378,7 +378,7 @@ def test_parameterized_repeat():
cirq.Simulator().simulate(cirq.Circuit(op), param_resolver={'a': 1.5})
with pytest.raises(ValueError, match='Circuit contains ops whose symbols were not specified'):
cirq.Simulator().simulate(cirq.Circuit(op))
op = op ** -1
op = op**-1
assert cirq.parameter_names(op) == {'a'}
assert not cirq.has_unitary(op)
result = cirq.Simulator().simulate(cirq.Circuit(op), param_resolver={'a': 0})
Expand Down Expand Up @@ -406,7 +406,7 @@ def test_parameterized_repeat():
cirq.Simulator().simulate(cirq.Circuit(op), param_resolver={'a': 1.5, 'b': 1})
with pytest.raises(ValueError, match='Circuit contains ops whose symbols were not specified'):
cirq.Simulator().simulate(cirq.Circuit(op))
op = op ** 2.0
op = op**2.0
assert cirq.parameter_names(op) == {'a', 'b'}
assert not cirq.has_unitary(op)
result = cirq.Simulator().simulate(cirq.Circuit(op), param_resolver={'a': 1, 'b': 1})
Expand Down
73 changes: 32 additions & 41 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def test_next_moment_operating_on_distance(circuit_cls):
assert c.next_moment_operating_on([a], 1, max_distance=500) == 4

# Huge max distances should be handled quickly due to capping.
assert c.next_moment_operating_on([a], 5, max_distance=10 ** 100) is None
assert c.next_moment_operating_on([a], 5, max_distance=10**100) is None

with pytest.raises(ValueError, match='Negative max_distance'):
c.next_moment_operating_on([a], 0, max_distance=-1)
Expand Down Expand Up @@ -1396,7 +1396,7 @@ def test_prev_moment_operating_on_distance(circuit_cls):
assert c.prev_moment_operating_on([a], 13, max_distance=500) == 1

# Huge max distances should be handled quickly due to capping.
assert c.prev_moment_operating_on([a], 1, max_distance=10 ** 100) is None
assert c.prev_moment_operating_on([a], 1, max_distance=10**100) is None

with pytest.raises(ValueError, match='Negative max_distance'):
c.prev_moment_operating_on([a], 6, max_distance=-1)
Expand Down Expand Up @@ -1703,7 +1703,7 @@ def test_findall_operations_until_blocked(circuit_cls):
)


@pytest.mark.parametrize('seed', [randint(0, 2 ** 31)])
@pytest.mark.parametrize('seed', [randint(0, 2**31)])
@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_findall_operations_until_blocked_docstring_examples(seed, circuit_cls):
prng = np.random.RandomState(seed)
Expand Down Expand Up @@ -2861,7 +2861,7 @@ def pauli_error_probability(r: float, n_qubits: int) -> float:
makes it simple to compute the serial composition of depolarizing channels. It
is multiplicative under channel composition.
"""
d2 = 4 ** n_qubits
d2 = 4**n_qubits
return (1 - r) * (d2 - 1) / d2

def depolarize(r: float, n_qubits: int) -> cirq.DepolarizingChannel:
Expand Down Expand Up @@ -4062,9 +4062,9 @@ def test_submoments(circuit_cls):
cirq.H.on(d),
cirq.CZ.on(a, d),
cirq.CZ.on(b, c),
(cirq.CNOT ** 0.5).on(a, d),
(cirq.CNOT ** 0.5).on(b, e),
(cirq.CNOT ** 0.5).on(c, f),
(cirq.CNOT**0.5).on(a, d),
(cirq.CNOT**0.5).on(b, e),
(cirq.CNOT**0.5).on(c, f),
cirq.H.on(c),
cirq.H.on(e),
)
Expand Down Expand Up @@ -4191,15 +4191,12 @@ def test_measurement_key_mapping(circuit_cls):
assert simulator.run(c).measurements == {'m1': 1, 'm2': 0}
assert simulator.run(c_swapped).measurements == {'m1': 0, 'm2': 1}

assert (
cirq.with_measurement_key_mapping(
c,
{
'x': 'z',
},
).all_measurement_key_names()
== {'m1', 'm2'}
)
assert cirq.with_measurement_key_mapping(
c,
{
'x': 'z',
},
).all_measurement_key_names() == {'m1', 'm2'}


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
Expand All @@ -4222,7 +4219,7 @@ def test_measurement_key_mapping_preserves_moments(circuit_cls):
@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_inverse(circuit_cls):
a, b = cirq.LineQubit.range(2)
forward = circuit_cls((cirq.X ** 0.5)(a), (cirq.Y ** -0.2)(b), cirq.CZ(a, b))
forward = circuit_cls((cirq.X**0.5)(a), (cirq.Y**-0.2)(b), cirq.CZ(a, b))
backward = circuit_cls((cirq.CZ ** (-1.0))(a, b), (cirq.X ** (-0.5))(a), (cirq.Y ** (0.2))(b))
cirq.testing.assert_same_circuits(cirq.inverse(forward), backward)

Expand All @@ -4233,15 +4230,15 @@ def test_inverse(circuit_cls):
cirq.inverse(no_inverse)

# Default when there is no inverse for an op.
default = circuit_cls((cirq.X ** 0.5)(a), (cirq.Y ** -0.2)(b))
default = circuit_cls((cirq.X**0.5)(a), (cirq.Y**-0.2)(b))
cirq.testing.assert_same_circuits(cirq.inverse(no_inverse, default), default)
assert cirq.inverse(no_inverse, None) is None


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_pow_valid_only_for_minus_1(circuit_cls):
a, b = cirq.LineQubit.range(2)
forward = circuit_cls((cirq.X ** 0.5)(a), (cirq.Y ** -0.2)(b), cirq.CZ(a, b))
forward = circuit_cls((cirq.X**0.5)(a), (cirq.Y**-0.2)(b), cirq.CZ(a, b))

backward = circuit_cls((cirq.CZ ** (-1.0))(a, b), (cirq.X ** (-0.5))(a), (cirq.Y ** (0.2))(b))
cirq.testing.assert_same_circuits(cirq.pow(forward, -1), backward)
Expand Down Expand Up @@ -4642,28 +4639,22 @@ def _measurement_key_name_(self):
assert circuit_cls().all_measurement_key_names() == set()

# Order does not matter.
assert (
circuit_cls(
cirq.Moment(
[
cirq.measure(a, key='x'),
cirq.measure(b, key='y'),
]
)
).all_measurement_key_names()
== {'x', 'y'}
)
assert (
circuit_cls(
cirq.Moment(
[
cirq.measure(b, key='y'),
cirq.measure(a, key='x'),
]
)
).all_measurement_key_names()
== {'x', 'y'}
)
assert circuit_cls(
cirq.Moment(
[
cirq.measure(a, key='x'),
cirq.measure(b, key='y'),
]
)
).all_measurement_key_names() == {'x', 'y'}
assert circuit_cls(
cirq.Moment(
[
cirq.measure(b, key='y'),
cirq.measure(a, key='x'),
]
)
).all_measurement_key_names() == {'x', 'y'}


def test_zip():
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def kraus_tensors(op: 'cirq.Operation') -> Sequence[np.ndarray]:
transpose = input_subscripts + '->' + output_subscripts

r = []
d = 2 ** n
d = 2**n
kss = [kraus_tensors(op) for op in self.operations]
for ks in itertools.product(*kss):
k = np.einsum(transpose, *ks)
Expand Down
8 changes: 4 additions & 4 deletions cirq-core/cirq/circuits/moment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ def test_json_dict():
def test_inverse():
a, b, c = cirq.LineQubit.range(3)
m = cirq.Moment([cirq.S(a), cirq.CNOT(b, c)])
assert m ** 1 is m
assert m ** -1 == cirq.Moment([cirq.S(a) ** -1, cirq.CNOT(b, c)])
assert m ** 0.5 == cirq.Moment([cirq.T(a), cirq.CNOT(b, c) ** 0.5])
assert cirq.inverse(m) == m ** -1
assert m**1 is m
assert m**-1 == cirq.Moment([cirq.S(a) ** -1, cirq.CNOT(b, c)])
assert m**0.5 == cirq.Moment([cirq.T(a), cirq.CNOT(b, c) ** 0.5])
assert cirq.inverse(m) == m**-1
assert cirq.inverse(cirq.inverse(m)) == m
assert cirq.inverse(cirq.Moment([cirq.measure(a)]), default=None) is None

Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/optimization_pass_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_point_optimizer_post_clean_up():

def clean_up(operations):
for op in operations:
yield op ** 0.5
yield op**0.5

ReplaceWithXGates(post_clean_up=clean_up)(c)

Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/acquaintance/executor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def random_diagonal_gates(
) -> Dict[Tuple[cirq.Qid, ...], cirq.Gate]:

return {
Q: cirq.DiagonalGate(np.random.random(2 ** acquaintance_size))
Q: cirq.DiagonalGate(np.random.random(2**acquaintance_size))
for Q in combinations(cirq.LineQubit.range(num_qubits), acquaintance_size)
}

Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/acquaintance/permutation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_linear_permutation_gate_pow_not_implemented():
def test_linear_permutation_gate_pow_identity(num_qubits, permutation):
permutation_gate = cca.LinearPermutationGate(num_qubits, permutation)

assert permutation_gate ** 1 == permutation_gate
assert permutation_gate**1 == permutation_gate


@pytest.mark.parametrize(
Expand All @@ -201,7 +201,7 @@ def test_linear_permutation_gate_pow_inverse(num_qubits, permutation, inverse):
permutation_gate = cca.LinearPermutationGate(num_qubits, permutation)
inverse_gate = cca.LinearPermutationGate(num_qubits, inverse)

assert permutation_gate ** -1 == inverse_gate
assert permutation_gate**-1 == inverse_gate
assert cirq.inverse(permutation_gate) == inverse_gate


Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/qasm_import/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ def __init__(self):
qasm_gate='cswap', num_params=0, num_args=3, cirq_gate=ops.CSWAP
),
'ccx': QasmGateStatement(qasm_gate='ccx', num_params=0, num_args=3, cirq_gate=ops.CCX),
'sdg': QasmGateStatement(qasm_gate='sdg', num_params=0, num_args=1, cirq_gate=ops.S ** -1),
'tdg': QasmGateStatement(qasm_gate='tdg', num_params=0, num_args=1, cirq_gate=ops.T ** -1),
'sdg': QasmGateStatement(qasm_gate='sdg', num_params=0, num_args=1, cirq_gate=ops.S**-1),
'tdg': QasmGateStatement(qasm_gate='tdg', num_params=0, num_args=1, cirq_gate=ops.T**-1),
}

all_gates = {**basic_gates, **qelib_gates}
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ def test_unknown_function():
('h', cirq.H),
('s', cirq.S),
('t', cirq.T),
('sdg', cirq.S ** -1),
('tdg', cirq.T ** -1),
('sdg', cirq.S**-1),
('tdg', cirq.T**-1),
('sx', cirq.XPowGate(exponent=0.5)),
]

Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/quantum_volume/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def compute_heavy_set(circuit: cirq.Circuit) -> List[int]:
# The output wave function is a vector from the result value (big-endian) to
# the probability of that bit-string. Return all of the bit-string
# values that have a probability greater than the median.
return [idx for idx, amp in enumerate(results.state_vector()) if np.abs(amp ** 2) > median]
return [idx for idx, amp in enumerate(results.state_vector()) if np.abs(amp**2) > median]


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/quirk/quirk_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ def z_to_quirk_op(gate: ops.ZPowGate) -> QuirkOp:


def cz_to_quirk_op(gate: ops.CZPowGate) -> Optional[QuirkOp]:
return z_to_quirk_op(ops.Z ** gate.exponent).controlled()
return z_to_quirk_op(ops.Z**gate.exponent).controlled()


def cnot_to_quirk_op(gate: ops.CXPowGate) -> Optional[QuirkOp]:
return x_to_quirk_op(ops.X ** gate.exponent).controlled()
return x_to_quirk_op(ops.X**gate.exponent).controlled()


def h_to_quirk_op(gate: ops.HPowGate) -> Optional[QuirkOp]:
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/routing/initialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_seeded_initial_mapping(graph_seed, init_seed):
return ccr.initialization.get_initial_mapping(logical_graph, device_graph, init_seed)


@pytest.mark.parametrize('seed', [random.randint(0, 2 ** 32) for _ in range(10)])
@pytest.mark.parametrize('seed', [random.randint(0, 2**32) for _ in range(10)])
def test_initialization_reproducible_with_seed(seed):
wrappers = (lambda s: s, np.random.RandomState)
mappings = [
Expand All @@ -39,7 +39,7 @@ def test_initialization_reproducible_with_seed(seed):
eq.add_equality_group(*mappings)


@pytest.mark.parametrize('graph_seed,state', [(random.randint(0, 2 ** 32), np.random.get_state())])
@pytest.mark.parametrize('graph_seed,state', [(random.randint(0, 2**32), np.random.get_state())])
def test_initialization_with_no_seed(graph_seed, state):
mappings = []
for _ in range(3):
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/routing/router_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def random_seed():
return random.randint(0, 2 ** 32)
return random.randint(0, 2**32)


@pytest.mark.parametrize(
Expand Down
12 changes: 6 additions & 6 deletions cirq-core/cirq/devices/noise_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ def test_constant_qubit_noise():
cirq.testing.assert_equivalent_repr(damp_all)

with pytest.raises(ValueError, match='num_qubits'):
_ = cirq.ConstantQubitNoiseModel(cirq.CNOT ** 0.01)
_ = cirq.ConstantQubitNoiseModel(cirq.CNOT**0.01)


def test_noise_composition():
# Verify that noise models can be composed without regard to ordering, as
# long as the noise operators commute with one another.
a, b, c = cirq.LineQubit.range(3)
noise_z = cirq.ConstantQubitNoiseModel(cirq.Z)
noise_inv_s = cirq.ConstantQubitNoiseModel(cirq.S ** -1)
noise_inv_s = cirq.ConstantQubitNoiseModel(cirq.S**-1)
base_moments = [cirq.Moment([cirq.X(a)]), cirq.Moment([cirq.Y(b)]), cirq.Moment([cirq.H(c)])]
circuit_z = cirq.Circuit(noise_z.noisy_moments(base_moments, [a, b, c]))
circuit_s = cirq.Circuit(noise_inv_s.noisy_moments(base_moments, [a, b, c]))
Expand All @@ -154,7 +154,7 @@ def test_noise_composition():


def test_constant_qubit_noise_repr():
cirq.testing.assert_equivalent_repr(cirq.ConstantQubitNoiseModel(cirq.X ** 0.01))
cirq.testing.assert_equivalent_repr(cirq.ConstantQubitNoiseModel(cirq.X**0.01))


def test_wrap():
Expand All @@ -168,16 +168,16 @@ def noisy_operation(self, operation):
assert cirq.NoiseModel.from_noise_model_like(
cirq.depolarize(0.1)
) == cirq.ConstantQubitNoiseModel(cirq.depolarize(0.1))
assert cirq.NoiseModel.from_noise_model_like(cirq.Z ** 0.01) == cirq.ConstantQubitNoiseModel(
cirq.Z ** 0.01
assert cirq.NoiseModel.from_noise_model_like(cirq.Z**0.01) == cirq.ConstantQubitNoiseModel(
cirq.Z**0.01
)
assert cirq.NoiseModel.from_noise_model_like(forget) is forget

with pytest.raises(TypeError, match='Expected a NOISE_MODEL_LIKE'):
_ = cirq.NoiseModel.from_noise_model_like('test')

with pytest.raises(ValueError, match='Multi-qubit gate'):
_ = cirq.NoiseModel.from_noise_model_like(cirq.CZ ** 0.01)
_ = cirq.NoiseModel.from_noise_model_like(cirq.CZ**0.01)


def test_gate_substitution_noise_model():
Expand Down
Loading