\n",
" **Package versions**

\n",
"\n",
" The code on this page was developed using the following requirements.\n",
" We recommend using these versions or newer.\n",
"\n",
" ```\n",
" qiskit[all]~=1.2.4\n",
" qiskit-aer~=0.15.1\n",
" qiskit-ibm-runtime~=0.31.0\n",
" qiskit-serverless~=0.17.1\n",
" qiskit-ibm-catalog~=0.1\n",
" ```\n",
"

\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "05fb8adb-3b36-4449-b669-108d2d749ca5",
"metadata": {},
"source": [
"This page describes some of the more commonly used parameters for local transpilation. These parameters are configured using arguments to [`generate_preset_pass_manager`](/api/qiskit/transpiler_preset#generate_preset_pass_manager) or [`transpile`](/api/qiskit/compiler#qiskit.compiler.transpile).\n",
"\n",
"\n",
"\n",
"## Approximation degree\n",
"\n",
"You can use the approximation degree to specify how closely you want the resultant circuit to match the desired (input) circuit. This is a float in the range (0.0 - 1.0), where 0.0 is maximum approximation and 1.0 (default) is no approximation. Smaller values trade output accuracy for ease of execution (that is, fewer gates). The default value is 1.0.\n",
"\n",
"In two-qubit unitary synthesis (used in initial stages of all levels and for optimization stage with optimization level 3), this value specifies the target fidelity of the output decomposition. That is, how much error is introduced when a matrix representation of a circuit is converted to discrete gates. If the approximation degree is a lower value (more approximation), the output circuit from synthesis will differ more from the input matrix, but will also likely have fewer gates (because any arbitrary two-qubit operation can be decomposed perfectly with at most three CX gates) and is easier to run.\n",
"\n",
"When the approximation degree is less than 1.0, circuits with one or two CX gates might be synthesized, leading to less error from the hardware, but more from the approximation. Since CX is the most expensive gate in terms of error, it might be beneficial to decrease the number of them at the cost of fidelity in synthesis (this technique was used to increase quantum volume on IBMÂ® devices: [Validating quantum computers using randomized model circuits](https://arxiv.org/abs/1811.12926)).\n",
"\n",
"As an example, we generate a random two-qubit `UnitaryGate` which will be synthesized in the initial stage. Setting the `approximation_degree` less than 1.0 might generate an approximate circuit. We must also specify the `basis_gates` to let the synthesis method know which gates it can use for the approximate synthesis.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e660bec2-f806-4050-955b-53a50289158e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"from qiskit import QuantumCircuit, QuantumRegister\n",
"from qiskit.circuit.library import UnitaryGate\n",
"from qiskit.quantum_info import random_unitary\n",
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"\n",
"UU = random_unitary(4, seed=12345)\n",
"rand_U = UnitaryGate(UU)\n",
"\n",
"qubits = QuantumRegister(2, name=\"q\")\n",
"qc = QuantumCircuit(qubits)\n",
"qc.append(rand_U, qubits)\n",
"pass_manager = generate_preset_pass_manager(\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\"])"
]
},
{
"cell_type": "markdown",
"id": "6f067d6e-0157-43fc-8cb2-c86855b6bdac",
"metadata": {},
"source": [
"This yields an output of `2` because the approximation requires fewer CX gates.\n",
"\n",
"\n",
"\n",
"## Random number generator seed\n",
"\n",
"Some parts of the transpiler are stochastic, so repeated transpilation runs may return different results. To obtain a reproducible result, you can set the seed for the pseudorandom number generator using the `seed_transpiler` argument. Repeated runs using the same seed will return the same results.\n",
"\n",
"Example:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dbc652e8-53a4-47a9-a66e-d9c1e5ef07c9",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
"