Skip to content

Commit

Permalink
Format with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Oct 29, 2024
1 parent fb14e64 commit 31c3652
Show file tree
Hide file tree
Showing 34 changed files with 538 additions and 363 deletions.
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 @@ -176,8 +176,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 @@ -492,7 +492,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 @@ -540,7 +540,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 @@ -718,9 +718,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 @@ -775,7 +775,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 @@ -825,35 +827,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 @@ -908,7 +924,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 @@ -52,13 +52,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 @@ -112,8 +113,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 @@ -172,8 +174,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 @@ -224,7 +228,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 @@ -279,7 +283,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 @@ -324,7 +328,8 @@
],
"source": [
"from qiskit.circuit.library import QuantumVolume\n",
"QuantumVolume(4).decompose().draw('mpl')"
"\n",
"QuantumVolume(4).decompose().draw(\"mpl\")"
]
},
{
Expand Down Expand Up @@ -375,25 +380,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 @@ -424,7 +433,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 @@ -48,7 +48,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 @@ -195,7 +195,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 @@ -240,7 +240,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 @@ -385,10 +385,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 @@ -424,9 +426,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
23 changes: 18 additions & 5 deletions docs/guides/create-transpiler-plugin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"from qiskit.transpiler.passes import VF2Layout\n",
"from qiskit.transpiler.passmanager_config import PassManagerConfig\n",
"from qiskit.transpiler.preset_passmanagers import common\n",
"from qiskit.transpiler.preset_passmanagers.plugin import PassManagerStagePlugin\n",
"from qiskit.transpiler.preset_passmanagers.plugin import (\n",
" PassManagerStagePlugin,\n",
")\n",
"\n",
"\n",
"class MyLayoutPlugin(PassManagerStagePlugin):\n",
Expand All @@ -84,7 +86,9 @@
" )\n",
" ]\n",
" )\n",
" layout_pm += common.generate_embed_passmanager(pass_manager_config.coupling_map)\n",
" layout_pm += common.generate_embed_passmanager(\n",
" pass_manager_config.coupling_map\n",
" )\n",
" return layout_pm"
]
},
Expand Down Expand Up @@ -178,7 +182,9 @@
"metadata": {},
"outputs": [],
"source": [
"from qiskit.transpiler.preset_passmanagers.plugin import PassManagerStagePluginManager\n",
"from qiskit.transpiler.preset_passmanagers.plugin import (\n",
" PassManagerStagePluginManager,\n",
")\n",
"\n",
"# Initialize the plugin manager\n",
"plugin_manager = PassManagerStagePluginManager()\n",
Expand Down Expand Up @@ -436,7 +442,12 @@
"\n",
"class MyCliffordSynthesisPlugin(HighLevelSynthesisPlugin):\n",
" def run(\n",
" self, high_level_object, coupling_map=None, target=None, qubits=None, **options\n",
" self,\n",
" high_level_object,\n",
" coupling_map=None,\n",
" target=None,\n",
" qubits=None,\n",
" **options,\n",
" ) -> QuantumCircuit:\n",
" if high_level_object.num_qubits <= 3:\n",
" return synth_clifford_bm(high_level_object)\n",
Expand Down Expand Up @@ -513,7 +524,9 @@
}
],
"source": [
"from qiskit.transpiler.passes.synthesis import high_level_synthesis_plugin_names\n",
"from qiskit.transpiler.passes.synthesis import (\n",
" high_level_synthesis_plugin_names,\n",
")\n",
"\n",
"high_level_synthesis_plugin_names(\"clifford\")"
]
Expand Down
Loading

0 comments on commit 31c3652

Please sign in to comment.