Skip to content

Commit

Permalink
Update black requirement from ~=19.10b0 to ~=22.1 (#1110)
Browse files Browse the repository at this point in the history
* Update black requirement from ~=19.10b0 to ~=22.1

Updates the requirements on [black](https://github.com/psf/black) to permit the latest version.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](psf/black@19.10b0...22.1.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>

* applying new black rules

* fixing flake

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Sarah Kaiser <[email protected]>
  • Loading branch information
dependabot[bot] and crazy4pi314 authored Feb 17, 2022
1 parent c0af3ba commit 7aa9b71
Show file tree
Hide file tree
Showing 36 changed files with 356 additions and 157 deletions.
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ amazon-braket-sdk~=1.14.0
pytest-xdist[psutil]~=2.5.0
pytest-cov~=3.0.0
flake8~=4.0.1
black~=19.10b0
black~=22.1
mypy~=0.931

# Documentation and examples.
Expand Down
19 changes: 10 additions & 9 deletions mitiq/benchmarks/ghz_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@


def generate_ghz_circuit(
n_qubits: int, return_type: Optional[str] = None,
n_qubits: int,
return_type: Optional[str] = None,
) -> QPROGRAM:
"""Returns a GHZ circuit ie a circuit that prepares an ``n_qubits`` GHZ state.
Args:
n_qubits: The number of qubits in the circuit.
return_type: String which specifies the type of the returned
circuits. See the keys of ``mitiq.SUPPORTED_PROGRAM_TYPES``
for options. If ``None``, the returned circuits have type
``cirq.Circuit``.
Args:
n_qubits: The number of qubits in the circuit.
return_type: String which specifies the type of the returned
circuits. See the keys of ``mitiq.SUPPORTED_PROGRAM_TYPES``
for options. If ``None``, the returned circuits have type
``cirq.Circuit``.
Returns:
A GHZ circuit acting on ``n_qubits`` qubits.
Returns:
A GHZ circuit acting on ``n_qubits`` qubits.
"""
if n_qubits <= 0:
raise ValueError(
Expand Down
10 changes: 8 additions & 2 deletions mitiq/benchmarks/randomized_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ def generate_rb_circuits(
]
else:
cfd_matrices = _two_qubit_clifford_matrices(
qubits[0], qubits[1], cliffords,
qubits[0],
qubits[1],
cliffords,
)
circuits = [
_random_two_q_clifford(
qubits[0], qubits[1], num_cliffords, cfd_matrices, cliffords,
qubits[0],
qubits[1],
num_cliffords,
cfd_matrices,
cliffords,
)
for _ in range(trials)
]
Expand Down
14 changes: 10 additions & 4 deletions mitiq/cdr/clifford_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def _replace(
return [
cirq.ops.rz(a).on(*q)
for (a, q) in zip(
clifford_angles, [op.qubits for op in non_clifford_ops],
clifford_angles,
[op.qubits for op in non_clifford_ops],
)
]

Expand Down Expand Up @@ -344,7 +345,10 @@ def _closest_clifford(angles: np.ndarray) -> float:


@np.vectorize
def _is_clifford_angle(angles: np.ndarray, tol: float = 10 ** -5,) -> bool:
def _is_clifford_angle(
angles: np.ndarray,
tol: float = 10**-5,
) -> bool:
"""Function to check if a given angle is Clifford.
Args:
Expand Down Expand Up @@ -376,7 +380,7 @@ def _angle_to_proximities(angle: np.ndarray, sigma: float) -> List[float]:
for exponent in range(4):
if exponent == 0:
exponent = 4
diff = np.linalg.norm(rz_matrix - s_matrix ** exponent)
diff = np.linalg.norm(rz_matrix - s_matrix**exponent)
dists.append(np.exp(-((diff / sigma) ** 2)))
return dists

Expand All @@ -400,7 +404,9 @@ def _angle_to_proximity(angle: np.ndarray, sigma: float) -> float:

@np.vectorize
def _probabilistic_angle_to_clifford(
angles: np.ndarray, sigma: float, random_state: np.random.RandomState,
angles: np.ndarray,
sigma: float,
random_state: np.random.RandomState,
) -> float:
"""Returns a Clifford angle sampled from the distribution
Expand Down
9 changes: 7 additions & 2 deletions mitiq/cdr/tests/test_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ def simulate(circuit: QPROGRAM) -> np.ndarray:
@pytest.mark.parametrize("random_state", [1, 2, 3, 4, 5])
def test_execute_with_cdr(circuit_type, fit_function, kwargs, random_state):
circuit = random_x_z_cnot_circuit(
LineQubit.range(2), n_moments=5, random_state=random_state,
LineQubit.range(2),
n_moments=5,
random_state=random_state,
)
circuit = convert_from_mitiq(circuit, circuit_type)
obs = Observable(PauliString("XZ"), PauliString("YY"))
Expand Down Expand Up @@ -125,7 +127,10 @@ def test_no_num_fit_parameters_with_custom_fit_raises_error():

def test_execute_with_cdr_using_clifford_circuit():
a, b = cirq.LineQubit.range(2)
clifCirc = cirq.Circuit(cirq.H.on(a), cirq.H.on(b),)
clifCirc = cirq.Circuit(
cirq.H.on(a),
cirq.H.on(b),
)
obs = Observable(PauliString("XZ"), PauliString("YY"))
cdr_value = execute_with_cdr(
clifCirc, observable=obs, executor=execute, simulator=simulate
Expand Down
4 changes: 3 additions & 1 deletion mitiq/cdr/tests/test_clifford_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ def test_generate_training_circuits_bad_methods():
def test_generate_training_circuits_with_clifford_circuit():
circuit = Circuit(cirq.ops.rx(0.0).on(cirq.LineQubit(0)))
assert generate_training_circuits(
circuit, num_training_circuits=2, fraction_non_clifford=0.0,
circuit,
num_training_circuits=2,
fraction_non_clifford=0.0,
) == [circuit, circuit]


Expand Down
3 changes: 2 additions & 1 deletion mitiq/interface/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ def new_scaling_function(

scaled_circuit.remove_final_measurements()
_transform_registers(
scaled_circuit, new_qregs=circuit.qregs, # type: ignore
scaled_circuit,
new_qregs=circuit.qregs, # type: ignore
)
if circuit.cregs and not scaled_circuit.cregs: # type: ignore
scaled_circuit.add_register(*circuit.cregs) # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion mitiq/interface/mitiq_braket/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def _translate_two_qubit_braket_instruction_to_cirq_operation(


def _translate_one_qubit_cirq_operation_to_braket_instruction(
op: Union[np.ndarray, cirq_ops.Operation], target: Optional[int] = None,
op: Union[np.ndarray, cirq_ops.Operation],
target: Optional[int] = None,
) -> List[Instruction]:
"""Translates a one-qubit Cirq operation to a (sequence of) Braket
instruction(s) according to the following rules:
Expand Down
8 changes: 6 additions & 2 deletions mitiq/interface/mitiq_braket/tests/test_conversions_braket.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_from_braket_three_qubit_gates():


def _rotation_of_pi_over_7(num_qubits):
matrix = np.identity(2 ** num_qubits)
matrix = np.identity(2**num_qubits)
matrix[0:2, 0:2] = [
[np.cos(np.pi / 7), np.sin(np.pi / 7)],
[-np.sin(np.pi / 7), np.cos(np.pi / 7)],
Expand Down Expand Up @@ -307,7 +307,11 @@ def test_to_from_braket_uncommon_two_qubit_gates(uncommon_gate):


@pytest.mark.parametrize(
"common_gate", [ops.TOFFOLI, ops.FREDKIN,],
"common_gate",
[
ops.TOFFOLI,
ops.FREDKIN,
],
)
def test_to_from_braket_common_three_qubit_gates(common_gate):
"""These gates should stay the same (i.e., not get decomposed) when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def test_non_consecutive_wires_error():
with qml.tape.QuantumTape() as tape:
qml.CNOT(wires=[0, 2])
with pytest.raises(
UnsupportedQuantumTapeError, match="contiguously pack",
UnsupportedQuantumTapeError,
match="contiguously pack",
):
from_pennylane(tape)

Expand Down
20 changes: 16 additions & 4 deletions mitiq/interface/mitiq_pyquil/tests/test_zne_mitiq_pyquil.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def noiseless_executor(program: pyquil.Program) -> float:

def test_run_factory():
(qp,) = benchmarks.generate_rb_circuits(
n_qubits=1, num_cliffords=TEST_DEPTH, trials=1, return_type="pyquil",
n_qubits=1,
num_cliffords=TEST_DEPTH,
trials=1,
return_type="pyquil",
)

fac = zne.inference.RichardsonFactory([1.0, 2.0, 3.0])
Expand All @@ -55,15 +58,21 @@ def test_run_factory():

def test_execute_with_zne():
(qp,) = benchmarks.generate_rb_circuits(
n_qubits=1, num_cliffords=TEST_DEPTH, trials=1, return_type="pyquil",
n_qubits=1,
num_cliffords=TEST_DEPTH,
trials=1,
return_type="pyquil",
)
result = zne.execute_with_zne(qp, noiseless_executor)
assert np.isclose(result, 1.0, atol=1e-5)


def test_mitigate_executor():
(qp,) = benchmarks.generate_rb_circuits(
n_qubits=1, num_cliffords=TEST_DEPTH, trials=1, return_type="pyquil",
n_qubits=1,
num_cliffords=TEST_DEPTH,
trials=1,
return_type="pyquil",
)

new_executor = zne.mitigate_executor(noiseless_executor)
Expand All @@ -78,7 +87,10 @@ def decorated_executor(qp: pyquil.Program) -> float:

def test_zne_decorator():
(qp,) = benchmarks.generate_rb_circuits(
n_qubits=1, num_cliffords=TEST_DEPTH, trials=1, return_type="pyquil",
n_qubits=1,
num_cliffords=TEST_DEPTH,
trials=1,
return_type="pyquil",
)

result = decorated_executor(qp)
Expand Down
5 changes: 4 additions & 1 deletion mitiq/interface/mitiq_qiskit/qiskit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def execute_with_shots(
"""

return execute_with_shots_and_noise(
circuit, obs, noise_model=None, shots=shots,
circuit,
obs,
noise_model=None,
shots=shots,
)


Expand Down
12 changes: 6 additions & 6 deletions mitiq/interface/mitiq_qiskit/tests/test_qiskit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@


def test_execute():
""" Tests the Qiskit wavefunction simulation executor returns
"""Tests the Qiskit wavefunction simulation executor returns
appropriate expectation value given an observable.
"""

Expand All @@ -47,7 +47,7 @@ def test_execute():


def test_execute_with_shots():
""" Tests the Qiskit wavefunction sampling simulation executor returns
"""Tests the Qiskit wavefunction sampling simulation executor returns
appropriate expectation value given an observable.
"""

Expand All @@ -66,7 +66,7 @@ def test_execute_with_shots():


def test_execute_with_depolarizing_noise_single_qubit():
""" Tests the noisy sampling executor across increasing levels
"""Tests the noisy sampling executor across increasing levels
of single qubit gate noise
"""

Expand All @@ -88,7 +88,7 @@ def test_execute_with_depolarizing_noise_single_qubit():


def test_execute_with_depolarizing_noise_two_qubit():
""" Tests the noisy sampling executor across increasing levels of
"""Tests the noisy sampling executor across increasing levels of
two qubit gate noise.
"""

Expand All @@ -110,7 +110,7 @@ def test_execute_with_depolarizing_noise_two_qubit():


def test_execute_with_shots_and_depolarizing_noise_single_qubit():
""" Tests the noisy sampling executor across increasing levels
"""Tests the noisy sampling executor across increasing levels
of single qubit gate noise.
"""

Expand All @@ -133,7 +133,7 @@ def test_execute_with_shots_and_depolarizing_noise_single_qubit():


def test_execute_with_shots_and_depolarizing_noise_two_qubit():
""" Tests the noisy sampling executor across increasing levels of
"""Tests the noisy sampling executor across increasing levels of
two qubit gate noise.
"""

Expand Down
4 changes: 2 additions & 2 deletions mitiq/observable/observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class Observable:
"""A quantum observable typically used to compute its mitigated expectation
value.
value.
"""

Expand Down Expand Up @@ -115,7 +115,7 @@ def matrix(
qubit_indices = self.qubit_indices
n = len(qubit_indices)

matrix = np.zeros(shape=(2 ** n, 2 ** n), dtype=dtype)
matrix = np.zeros(shape=(2**n, 2**n), dtype=dtype)
for pauli in self._paulis:
matrix += pauli.matrix(
qubit_indices_to_include=qubit_indices
Expand Down
28 changes: 22 additions & 6 deletions mitiq/observable/tests/test_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,41 @@ def test_expectation_from_measurements_identity(seed, nqubits):
rng.randint(low=0, high=1 + 1, size=(100, nqubits)).tolist()
)
assert np.isclose(
pauli._expectation_from_measurements(measurements), coeff,
pauli._expectation_from_measurements(measurements),
coeff,
)


def test_expectation_from_measurements_two_qubits():
measurements = MeasurementResult([[0, 1] * 1_000])

z0 = PauliString(spec="Z", support=(0,))
assert np.isclose(z0._expectation_from_measurements(measurements), 1.0,)
assert np.isclose(
z0._expectation_from_measurements(measurements),
1.0,
)
zi = PauliString(spec="ZI")
assert np.isclose(zi._expectation_from_measurements(measurements), 1.0,)
assert np.isclose(
zi._expectation_from_measurements(measurements),
1.0,
)

z1 = PauliString(spec="Z", support=(1,))
assert np.isclose(z1._expectation_from_measurements(measurements), -1.0,)
assert np.isclose(
z1._expectation_from_measurements(measurements),
-1.0,
)
iz = PauliString(spec="IZ")
assert np.isclose(iz._expectation_from_measurements(measurements), -1.0,)
assert np.isclose(
iz._expectation_from_measurements(measurements),
-1.0,
)

zz = PauliString(spec="ZZ")
assert np.isclose(zz._expectation_from_measurements(measurements), -1.0,)
assert np.isclose(
zz._expectation_from_measurements(measurements),
-1.0,
)


def test_pstringcollection():
Expand Down
7 changes: 4 additions & 3 deletions mitiq/pec/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ def matrix_to_vector(density_matrix: np.ndarray) -> np.ndarray:


def _safe_sqrt(
perfect_square: int, error_str: str = "The input must be a square number.",
perfect_square: int,
error_str: str = "The input must be a square number.",
) -> int:
"""Takes the square root of the input integer and
raises an error if the input is not a perfect square."""
square_root = int(np.round(np.sqrt(perfect_square)))
if square_root ** 2 != perfect_square:
if square_root**2 != perfect_square:
raise ValueError(error_str)
return square_root

Expand Down Expand Up @@ -182,7 +183,7 @@ def choi_to_super(choi_state: np.ndarray) -> np.ndarray:

choi_kl_ij = choi_state.reshape(dim, dim, dim, dim)
choi_ki_lj = choi_kl_ij.transpose(0, 2, 1, 3)
super_not_normalized = choi_ki_lj.reshape(dim ** 2, dim ** 2)
super_not_normalized = choi_ki_lj.reshape(dim**2, dim**2)
return dim * super_not_normalized


Expand Down
Loading

0 comments on commit 7aa9b71

Please sign in to comment.