Skip to content

Commit

Permalink
Format notebooks with Ruff (#2199)
Browse files Browse the repository at this point in the history
Follow-up to #2197. Draft until that PR is merged, then I'll rebase
this.

Also a partial fix for #1806 depending if we want to also lint code
examples in MDX.
  • Loading branch information
frankharkins authored Nov 4, 2024
1 parent ff75179 commit 9d98cb0
Show file tree
Hide file tree
Showing 38 changed files with 557 additions and 362 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ change, CI will alert us.
### Lint notebooks
We use [`squeaky`](https://github.com/frankharkins/squeaky) to lint our
notebooks. First install `tox` using [pipx](https://pipx.pypa.io/stable/).
We use [`squeaky`](https://github.com/frankharkins/squeaky) and
[`ruff`](https://docs.astral.sh/ruff/) to lint our notebooks. First install
`tox` using [pipx](https://pipx.pypa.io/stable/).
```sh
pipx install tox
Expand All @@ -206,19 +207,19 @@ To check if a notebook needs linting:
```sh
# Check all notebooks in ./docs
tox -e lint -- docs/**/*.ipynb
tox -e lint
```
To fix problems in a notebooks, run:
Some problems can be fixed automatically. To fix these problems, run:
```sh
# Fix problems in all notebooks
tox -e fix
# Fix problems in a specific notebook
tox -e fix -- path/to/notebook
```
Or, you can retrieve an executed and linted version of your notebook from CI
following the steps at the end of the [Execute notebooks](#execute-notebooks)
section.
If you use the Jupyter notebook editor, consider adding squeaky as a [pre-save
hook](https://github.com/frankharkins/squeaky?tab=readme-ov-file#jupyter-pre-save-hook).
This will lint your notebooks as you save them, so you never need to worry
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/algorithmiq-tem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@
"pub = (qc, [observable])\n",
"options = {\"default_precision\": 0.02}\n",
"\n",
"job = tem.run(pubs=[pub], instance=instance, backend_name=backend_name, options=options)"
"job = tem.run(\n",
" pubs=[pub], instance=instance, backend_name=backend_name, options=options\n",
")"
]
},
{
Expand Down
74 changes: 46 additions & 28 deletions docs/guides/build-noise-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@
"source": [
"# Construct a 1-qubit bit-flip and phase-flip errors\n",
"p_error = 0.05\n",
"bit_flip = pauli_error([('X', p_error), ('I', 1 - p_error)])\n",
"phase_flip = pauli_error([('Z', p_error), ('I', 1 - p_error)])\n",
"bit_flip = pauli_error([(\"X\", p_error), (\"I\", 1 - p_error)])\n",
"phase_flip = pauli_error([(\"Z\", p_error), (\"I\", 1 - p_error)])\n",
"print(bit_flip)\n",
"print(phase_flip)"
]
Expand Down Expand Up @@ -523,7 +523,7 @@
"\n",
"# Add depolarizing error to all single qubit u1, u2, u3 gates\n",
"error = depolarizing_error(0.05, 1)\n",
"noise_model.add_all_qubit_quantum_error(error, ['u1', 'u2', 'u3'])\n",
"noise_model.add_all_qubit_quantum_error(error, [\"u1\", \"u2\", \"u3\"])\n",
"\n",
"# Print noise model info\n",
"print(noise_model)"
Expand Down Expand Up @@ -571,7 +571,7 @@
"\n",
"# Add depolarizing error to all single qubit u1, u2, u3 gates on qubit 0 only\n",
"error = depolarizing_error(0.05, 1)\n",
"noise_model.add_quantum_error(error, ['u1', 'u2', 'u3'], [0])\n",
"noise_model.add_quantum_error(error, [\"u1\", \"u2\", \"u3\"], [0])\n",
"\n",
"# Print noise model info\n",
"print(noise_model)"
Expand Down Expand Up @@ -749,9 +749,9 @@
"p_gate1 = 0.05\n",
"\n",
"# QuantumError objects\n",
"error_reset = pauli_error([('X', p_reset), ('I', 1 - p_reset)])\n",
"error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)])\n",
"error_gate1 = pauli_error([('X',p_gate1), ('I', 1 - p_gate1)])\n",
"error_reset = pauli_error([(\"X\", p_reset), (\"I\", 1 - p_reset)])\n",
"error_meas = pauli_error([(\"X\", p_meas), (\"I\", 1 - p_meas)])\n",
"error_gate1 = pauli_error([(\"X\", p_gate1), (\"I\", 1 - p_gate1)])\n",
"error_gate2 = error_gate1.tensor(error_gate1)\n",
"\n",
"# Add errors to noise model\n",
Expand Down Expand Up @@ -806,7 +806,9 @@
"sim_noise = AerSimulator(noise_model=noise_bit_flip)\n",
"\n",
"# Transpile circuit for noisy basis gates\n",
"passmanager = generate_preset_pass_manager(optimization_level=3, backend=sim_noise)\n",
"passmanager = generate_preset_pass_manager(\n",
" optimization_level=3, backend=sim_noise\n",
")\n",
"circ_tnoise = passmanager.run(circ)\n",
"\n",
"# Run and get counts\n",
Expand Down Expand Up @@ -856,35 +858,49 @@
],
"source": [
"# T1 and T2 values for qubits 0-3\n",
"T1s = np.random.normal(50e3, 10e3, 4) # Sampled from normal distribution mean 50 microsec\n",
"T2s = np.random.normal(70e3, 10e3, 4) # Sampled from normal distribution mean 50 microsec\n",
"T1s = np.random.normal(\n",
" 50e3, 10e3, 4\n",
") # Sampled from normal distribution mean 50 microsec\n",
"T2s = np.random.normal(\n",
" 70e3, 10e3, 4\n",
") # Sampled from normal distribution mean 50 microsec\n",
"\n",
"# Truncate random T2s <= T1s\n",
"T2s = np.array([min(T2s[j], 2 * T1s[j]) for j in range(4)])\n",
"\n",
"# Instruction times (in nanoseconds)\n",
"time_u1 = 0 # virtual gate\n",
"time_u1 = 0 # virtual gate\n",
"time_u2 = 50 # (single X90 pulse)\n",
"time_u3 = 100 # (two X90 pulses)\n",
"time_u3 = 100 # (two X90 pulses)\n",
"time_cx = 300\n",
"time_reset = 1000 # 1 microsecond\n",
"time_measure = 1000 # 1 microsecond\n",
"time_measure = 1000 # 1 microsecond\n",
"\n",
"# QuantumError objects\n",
"errors_reset = [thermal_relaxation_error(t1, t2, time_reset)\n",
" for t1, t2 in zip(T1s, T2s)]\n",
"errors_measure = [thermal_relaxation_error(t1, t2, time_measure)\n",
" for t1, t2 in zip(T1s, T2s)]\n",
"errors_u1 = [thermal_relaxation_error(t1, t2, time_u1)\n",
" for t1, t2 in zip(T1s, T2s)]\n",
"errors_u2 = [thermal_relaxation_error(t1, t2, time_u2)\n",
" for t1, t2 in zip(T1s, T2s)]\n",
"errors_u3 = [thermal_relaxation_error(t1, t2, time_u3)\n",
" for t1, t2 in zip(T1s, T2s)]\n",
"errors_cx = [[thermal_relaxation_error(t1a, t2a, time_cx).expand(\n",
" thermal_relaxation_error(t1b, t2b, time_cx))\n",
" for t1a, t2a in zip(T1s, T2s)]\n",
" for t1b, t2b in zip(T1s, T2s)]\n",
"errors_reset = [\n",
" thermal_relaxation_error(t1, t2, time_reset) for t1, t2 in zip(T1s, T2s)\n",
"]\n",
"errors_measure = [\n",
" thermal_relaxation_error(t1, t2, time_measure) for t1, t2 in zip(T1s, T2s)\n",
"]\n",
"errors_u1 = [\n",
" thermal_relaxation_error(t1, t2, time_u1) for t1, t2 in zip(T1s, T2s)\n",
"]\n",
"errors_u2 = [\n",
" thermal_relaxation_error(t1, t2, time_u2) for t1, t2 in zip(T1s, T2s)\n",
"]\n",
"errors_u3 = [\n",
" thermal_relaxation_error(t1, t2, time_u3) for t1, t2 in zip(T1s, T2s)\n",
"]\n",
"errors_cx = [\n",
" [\n",
" thermal_relaxation_error(t1a, t2a, time_cx).expand(\n",
" thermal_relaxation_error(t1b, t2b, time_cx)\n",
" )\n",
" for t1a, t2a in zip(T1s, T2s)\n",
" ]\n",
" for t1b, t2b in zip(T1s, T2s)\n",
"]\n",
"\n",
"# Add errors to noise model\n",
"noise_thermal = NoiseModel()\n",
Expand Down Expand Up @@ -939,7 +955,9 @@
"sim_thermal = AerSimulator(noise_model=noise_thermal)\n",
"\n",
"# Transpile circuit for noisy basis gates\n",
"passmanager = generate_preset_pass_manager(optimization_level=3, backend=sim_thermal)\n",
"passmanager = generate_preset_pass_manager(\n",
" optimization_level=3, backend=sim_thermal\n",
")\n",
"circ_tthermal = passmanager.run(circ)\n",
"\n",
"# Run and get counts\n",
Expand Down
41 changes: 25 additions & 16 deletions docs/guides/circuit-library.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@
"source": [
"from qiskit import QuantumCircuit\n",
"from qiskit.circuit.library import HGate, MCXGate\n",
"\n",
"mcx_gate = MCXGate(3)\n",
"hadamard_gate = HGate()\n",
"\n",
"qc = QuantumCircuit(4)\n",
"qc.append(hadamard_gate, [0])\n",
"qc.append(mcx_gate, [0,1,2,3])\n",
"qc.draw('mpl')"
"qc.append(mcx_gate, [0, 1, 2, 3])\n",
"qc.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -141,8 +142,9 @@
],
"source": [
"from qiskit.circuit.library import TwoLocal\n",
"two_local = TwoLocal(3, 'rx', 'cz')\n",
"two_local.decompose().draw('mpl')"
"\n",
"two_local = TwoLocal(3, \"rx\", \"cz\")\n",
"two_local.decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -203,8 +205,10 @@
}
],
"source": [
"bound_circuit = two_local.assign_parameters({ p: 0 for p in two_local.parameters})\n",
"bound_circuit.decompose().draw('mpl')"
"bound_circuit = two_local.assign_parameters(\n",
" {p: 0 for p in two_local.parameters}\n",
")\n",
"bound_circuit.decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -257,7 +261,7 @@
"feature_map = ZZFeatureMap(feature_dimension=len(features))\n",
"\n",
"encoded = feature_map.assign_parameters(features)\n",
"encoded.draw('mpl')"
"encoded.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -314,7 +318,7 @@
"# Evolve state by appending the evolution gate\n",
"state.compose(evolution, inplace=True)\n",
"\n",
"state.draw('mpl')"
"state.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -361,7 +365,8 @@
],
"source": [
"from qiskit.circuit.library import QuantumVolume\n",
"QuantumVolume(4).decompose().draw('mpl')"
"\n",
"QuantumVolume(4).decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -414,25 +419,29 @@
"adder = CDKMRippleCarryAdder(3) # Adder of 3-bit numbers\n",
"\n",
"# Create the number A=2\n",
"reg_a = QuantumRegister(3, 'a')\n",
"reg_a = QuantumRegister(3, \"a\")\n",
"number_a = QuantumCircuit(reg_a)\n",
"number_a.initialize(2) # Number 2; |010>\n",
"number_a.initialize(2) # Number 2; |010>\n",
"\n",
"# Create the number B=3\n",
"reg_b = QuantumRegister(3, 'b')\n",
"reg_b = QuantumRegister(3, \"b\")\n",
"number_b = QuantumCircuit(reg_b)\n",
"number_b.initialize(3) # Number 3; |011>\n",
"\n",
"# Create a circuit to hold everything, including a classical register for\n",
"# the result\n",
"# the result\n",
"reg_result = ClassicalRegister(3)\n",
"circuit = QuantumCircuit(*adder.qregs, reg_result)\n",
"\n",
"# Compose number initializers with the adder. Adder stores the result to\n",
"# register B, so we'll measure those qubits.\n",
"circuit = circuit.compose(number_a, qubits=reg_a).compose(number_b, qubits=reg_b).compose(adder)\n",
"circuit = (\n",
" circuit.compose(number_a, qubits=reg_a)\n",
" .compose(number_b, qubits=reg_b)\n",
" .compose(adder)\n",
")\n",
"circuit.measure(reg_b, reg_result)\n",
"circuit.draw('mpl')"
"circuit.draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -463,7 +472,7 @@
"\n",
"result = StatevectorSampler().run([circuit]).result()\n",
"\n",
"print(f'Count data:\\n {result[0].data.c0.get_int_counts()}')"
"print(f\"Count data:\\n {result[0].data.c0.get_int_counts()}\")"
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/common-parameters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
"qc = QuantumCircuit(qubits)\n",
"qc.append(rand_U, qubits)\n",
"pass_manager = generate_preset_pass_manager(\n",
" optimization_level=1, approximation_degree=0.85, basis_gates=[\"sx\", \"rz\", \"cx\"]\n",
" optimization_level=1,\n",
" approximation_degree=0.85,\n",
" basis_gates=[\"sx\", \"rz\", \"cx\"],\n",
")\n",
"approx_qc = pass_manager.run(qc)\n",
"print(approx_qc.count_ops()[\"cx\"])"
Expand Down
14 changes: 7 additions & 7 deletions docs/guides/construct-circuits.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
"qc = QuantumCircuit(1)\n",
"qc.append(\n",
" HGate(), # New HGate instruction\n",
" [0] # Apply to qubit 0\n",
" [0], # Apply to qubit 0\n",
")\n",
"qc.draw(\"mpl\")"
]
Expand Down Expand Up @@ -265,7 +265,7 @@
"qc_b.y(0)\n",
"qc_b.z(1)\n",
"\n",
"# compose qubits (0, 1) of qc_a to qubits (1, 3) of qc_b respectively\n",
"# compose qubits (0, 1) of qc_a to qubits (1, 3) of qc_b respectively\n",
"combined = qc_a.compose(qc_b, qubits=[1, 3])\n",
"combined.draw(\"mpl\")"
]
Expand Down Expand Up @@ -410,10 +410,12 @@
"\n",
"angle = Parameter(\"angle\") # undefined number\n",
"\n",
"# Create and optimize circuit once\n",
"# Create and optimize circuit once\n",
"qc = QuantumCircuit(1)\n",
"qc.rx(angle, 0)\n",
"qc = generate_preset_pass_manager(optimization_level=3, basis_gates=['u', 'cx']).run(qc)\n",
"qc = generate_preset_pass_manager(\n",
" optimization_level=3, basis_gates=[\"u\", \"cx\"]\n",
").run(qc)\n",
"\n",
"qc.draw(\"mpl\")"
]
Expand Down Expand Up @@ -449,9 +451,7 @@
"source": [
"circuits = []\n",
"for value in range(100):\n",
" circuits.append(\n",
" qc.assign_parameters({ angle: value })\n",
" )\n",
" circuits.append(qc.assign_parameters({angle: value}))\n",
"\n",
"circuits[0].draw(\"mpl\")"
]
Expand Down
Loading

0 comments on commit 9d98cb0

Please sign in to comment.