qp.capture.expand_plxpr_transforms¶
- expand_plxpr_transforms(f)[source]¶
Function for applying transforms to plxpr.
Currently, when program capture is enabled, transforms are used as higher-order primitives. These primitives are present in the program, but their respective transform is not applied when a transformed function is called.
expand_plxpr_transformsfurther “transforms” the input function to apply any transform primitives that are present in the program being run.Example
In the below example, we can see that the
qp.transforms.cancel_inversestransform has been applied to a function. However, the resulting program representation leaves thecancel_inversestransform as a primitive without actually transforming the program.qp.capture.enable() @qp.transforms.cancel_inverses def circuit(): qp.X(0) qp.S(1) qp.X(0) qp.adjoint(qp.S(1)) return qp.expval(qp.Z(1))
>>> qp.capture.make_plxpr(circuit)() { lambda ; . let a:AbstractMeasurement(n_wires=None) = cancel_inverses_transform[ args_slice=slice(0, 0, None) consts_slice=slice(0, 0, None) inner_jaxpr={ lambda ; . let _:AbstractOperator() = PauliX[n_wires=1] 0 _:AbstractOperator() = S[n_wires=1] 1 _:AbstractOperator() = PauliX[n_wires=1] 0 b:AbstractOperator() = S[n_wires=1] 1 _:AbstractOperator() = Adjoint b c:AbstractOperator() = PauliZ[n_wires=1] 1 d:AbstractMeasurement(n_wires=None) = expval_obs c in (d,) } targs_slice=slice(0, None, None) tkwargs={} ] in (a,) }
To apply the transform, we can use
expand_plxpr_transformsas follows:>>> transformed_circuit = qp.capture.expand_plxpr_transforms(circuit) >>> qp.capture.make_plxpr(transformed_circuit)() { lambda ; . let a:AbstractOperator() = PauliZ[n_wires=1] 1 b:AbstractMeasurement(n_wires=None) = expval_obs a in (b,) }
As seen, the transform primitive is no longer present, but it has been applied to the original program, indicated by the inverse operators being cancelled.
- Parameters:
f (Callable) – The callable to which any present transforms should be applied.
- Returns:
Callable with transforms applied.
- Return type:
Callable