Skip to main contentIBM Quantum Documentation Mirror

WrapAngles

class qiskit.transpiler.passes.WrapAngles(*args, **kwargs)

GitHub

Bases: TransformationPass

Wrap angles outside the bound specified in the target.

This pass will check all the gates in the circuit and check if there are any gates outside the bound specified in the target. If any gates outside the bound are identified, the callback in the target will be called to substitute the gate outside the bound with an equivalent subcircuit. This pass does not run on gates that are parameterized, even if the gate has unparameterized parameters outside a specified bound. If there are parameterized gates in the circuit they will be ignored by this pass as bound angles are necessary to transform the gate. For example the below example demonstrates how the callback mechanism and registration works, but doesn’t show a useful transformation, but is simple to follow:

from qiskit.circuit import Gate, Parameter, Qubit, QuantumCircuit
from qiskit.circuit.library import RZGate
from qiskit.dagcircuit import DAGCircuit
from qiskit.transpiler.passes import WrapAngles
from qiskit.transpiler.target import Target
from qiskit.transpiler import WrapAngleRegistry
 
param = Parameter("a")
circuit = QuantumCircuit(1)
circuit.rz(6.8, 0)
target = Target(num_qubits=1)
target.add_instruction(RZGate(param), angle_bounds=[(0, 0.5)])
 
def callback(angles, _qubits):
    angle = angles[0]
    if angle > 0:
        number_of_gates = angle / 0.5
    else:
        number_of_gates = (6.28 - angle) / 0.5
    dag = DAGCircuit()
    dag.add_qubits([Qubit()])
    for _ in range(int(number_of_gates)):
        dag.apply_operation_back(RZGate(0.5), [dag.qubits[0]])
    return dag
 
registry = WrapAngleRegistry()
registry.add_wrapper("rz", callback)
wrap_pass = WrapAngles(target, registry)
res = wrap_pass(circuit)
res.draw("mpl")
Circuit digram of the output from running the WrapAngles pass

Parameters

  • target (Target) – The Target that
  • registry (WrapAngleRegistry) – The registry of wrapping functions used by the pass to wrap the angles of a gate. If not specified the global WRAP_ANGLE_REGISTRY object defined in this module will be used. Unless you are planning to run this pass standalone or are building a custom PassManager including this pass you will want to rely on WRAP_ANGLE_REGISTRY.

Attributes

is_analysis_pass

Check if the pass is an analysis pass.

If the pass is an AnalysisPass, that means that the pass can analyze the DAG and write the results of that analysis in the property set. Modifications on the DAG are not allowed by this kind of pass.

is_transformation_pass

Check if the pass is a transformation pass.

If the pass is a TransformationPass, that means that the pass can manipulate the DAG, but cannot modify the property set (but it can be read).


Methods

execute

execute(passmanager_ir, state, callback=None)

GitHub

Execute optimization task for input Qiskit IR.

Parameters

  • passmanager_ir (Any) – Qiskit IR to optimize.
  • state (PassManagerState) – State associated with workflow execution by the pass manager itself.
  • callback (Callable | None) – A callback function which is caller per execution of optimization task.

Returns

Optimized Qiskit IR and state of the workflow.

Return type

tuple[Any, qiskit.passmanager.compilation_status.PassManagerState]

name

name()

GitHub

Name of the pass.

Return type

str

run

run(dag)

GitHub

Run a pass on the DAGCircuit. This is implemented by the pass developer.

Parameters

dag – the dag on which the pass is run.

Raises

NotImplementedError – when this is left unimplemented for a pass.

update_status

update_status(state, run_state)

GitHub

Update workflow status.

Parameters

  • state (PassManagerState) – Pass manager state to update.
  • run_state (RunState) – Completion status of current task.

Returns

Updated pass manager state.

Return type

PassManagerState