qml.transforms

This subpackage contains QNode, quantum function, device, and tape transforms.

Transforms

Transforms that act on QNodes

These transforms accept QNodes, and return new transformed functions that compute the desired quantity.

classical_jacobian(qnode[, argnum, …])

Returns a function to extract the Jacobian matrix of the classical part of a QNode.

batch_params(tape[, all_operations])

Transform a QNode to support an initial batch dimension for operation parameters.

batch_input(tape, argnum)

Transform a QNode to support an initial batch dimension for gate inputs.

batch_partial(qnode[, all_operations, …])

Create a batched partial callable object from the QNode specified.

metric_tensor(tape[, argnum, approx, …])

Returns a function that computes the metric tensor of a given QNode or quantum tape.

adjoint_metric_tensor(circuit[, device, hybrid])

Implements the adjoint method outlined in Jones to compute the metric tensor.

specs(qnode[, max_expansion, expansion_strategy])

Resource information about a quantum circuit.

split_non_commuting(tape)

Splits a qnode measuring non-commuting observables into groups of commuting observables.

Transforms that act on quantum functions

These transforms accept quantum functions (Python functions containing quantum operations) that are used to construct QNodes.

cond(condition, true_fn[, false_fn])

Condition a quantum operation on the results of mid-circuit qubit measurements.

defer_measurements(tape)

Quantum function transform that substitutes operations conditioned on measurement outcomes to controlled operations.

apply_controlled_Q(fn, wires, target_wire, …)

Provides the circuit to apply a controlled version of the \(\mathcal{Q}\) unitary defined in this paper.

quantum_monte_carlo(fn, wires, target_wire, …)

Provides the circuit to perform the quantum Monte Carlo estimation algorithm.

insert(circuit, op, op_args[, position, before])

Insert an operation into specified points in an input circuit.

Transforms for circuit compilation

This set of transforms accept quantum functions, and perform basic circuit compilation tasks.

compile(tape[, pipeline, basis_set, …])

Compile a circuit by applying a series of transforms to a quantum function.

cancel_inverses(tape)

Quantum function transform to remove any operations that are applied next to their (self-)inverses or adjoint.

commute_controlled(tape[, direction])

Quantum function transform to move commuting gates past control and target qubits of controlled operations.

merge_rotations(tape[, atol, include_gates])

Quantum function transform to combine rotation gates of the same type that act sequentially.

single_qubit_fusion(tape[, atol, exclude_gates])

Quantum function transform to fuse together groups of single-qubit operations into a general single-qubit unitary operation (Rot).

unitary_to_rot(tape)

Quantum function transform to decomposes all instances of single-qubit and select instances of two-qubit QubitUnitary operations to parametrized single-qubit operations.

merge_amplitude_embedding(tape)

Quantum function transform to combine amplitude embedding templates that act on different qubits.

remove_barrier(tape)

Quantum function transform to remove Barrier gates.

undo_swaps(tape)

Quantum function transform to remove SWAP gates by running from right to left through the circuit changing the position of the qubits accordingly.

pattern_matching_optimization(tape, …[, …])

Quantum function transform to optimize a circuit given a list of patterns (templates).

transpile(tape, coupling_map)

Transpile a circuit according to a desired coupling map

There are also utility functions and decompositions available that assist with both transforms, and decompositions within the larger PennyLane codebase.

one_qubit_decomposition(U, wire[, …])

Decompose a one-qubit unitary \(U\) in terms of elementary operations.

two_qubit_decomposition(U, wires)

Decompose a two-qubit unitary \(U\) in terms of elementary operations.

set_decomposition(custom_decomps, dev[, …])

Context manager for setting custom decompositions.

pattern_matching(circuit_dag, pattern_dag)

Function that applies the pattern matching algorithm and returns the list of maximal matches.

to_zx(tape[, expand_measurement])

This transform converts a PennyLane quantum tape to a ZX-Graph in the PyZX framework.

from_zx(graph[, decompose_phases])

Converts a graph from PyZX to a PennyLane tape, if the graph is diagram-like.

There are also utility functions that take a circuit and return a DAG.

commutation_dag(circuit)

Construct the pairwise-commutation DAG (directed acyclic graph) representation of a quantum circuit.

CommutationDAG(tape)

Class to represent a quantum circuit as a directed acyclic graph (DAG).

CommutationDAGNode([op, wires, …])

Class to store information about a quantum operation in a node of the commutation DAG.

Transform for circuit cutting

The cut_circuit() transform accepts a QNode and returns a new function that cuts the original circuit, allowing larger circuits to be split into smaller circuits that are compatible with devices that have a restricted number of qubits.

cut_circuit(tape[, auto_cutter, …])

Cut up a quantum circuit into smaller circuit fragments.

The cut_circuit_mc() transform is designed to be used for cutting circuits which contain sample() measurements and is implemented using a Monte Carlo method. Similarly to the cut_circuit() transform, this transform accepts a QNode and returns a new function that cuts the original circuit. This transform can also accept an optional classical processing function to calculate an expectation value.

cut_circuit_mc(tape[, …])

Cut up a circuit containing sample measurements into smaller fragments using a Monte Carlo method.

There are also low-level functions that can be used to build up the circuit cutting functionalities:

tape_to_graph(tape)

Converts a quantum tape to a directed multigraph.

replace_wire_cut_nodes(graph)

Replace each WireCut node in the graph with a MeasureNode and PrepareNode.

fragment_graph(graph)

Fragments a graph into a collection of subgraphs as well as returning the communication (quotient) graph.

graph_to_tape(graph)

Converts a directed multigraph to the corresponding QuantumTape.

expand_fragment_tape(tape)

Expands a fragment tape into a sequence of tapes for each configuration of the contained MeasureNode and PrepareNode operations.

expand_fragment_tapes_mc(tapes, …)

Expands fragment tapes into a sequence of random configurations of the contained pairs of MeasureNode and PrepareNode operations.

qcut_processing_fn(results, …[, …])

Processing function for the cut_circuit() transform.

qcut_processing_fn_sample(results, …)

Function to postprocess samples for the cut_circuit_mc() transform.

qcut_processing_fn_mc(results, …)

Function to postprocess samples for the cut_circuit_mc() transform.

CutStrategy([devices, max_free_wires, …])

A circuit-cutting distribution policy for executing (large) circuits on available (comparably smaller) devices.

kahypar_cut(graph, num_fragments[, …])

Calls KaHyPar to partition a graph.

place_wire_cuts(graph, cut_edges)

Inserts a WireCut node for each provided cut edge into a circuit graph.

find_and_place_cuts(graph[, cut_method, …])

Automatically finds and places optimal WireCut nodes into a given tape-converted graph using a customizable graph partitioning function.

Transforms that act on tapes

These transforms accept quantum tapes, and return one or more tapes as well as a classical processing function.

broadcast_expand(tape)

Expand a broadcasted tape into multiple tapes and a function that stacks and squeezes the results.

hamiltonian_expand(tape[, group])

Splits a tape measuring a Hamiltonian expectation into mutliple tapes of Pauli expectations, and provides a function to recombine the results.

sign_expand(tape[, circuit, J, delta, controls])

Splits a tape measuring a (fast-forwardable) Hamiltonian expectation into mutliple tapes of the Xi or sgn decomposition, and provides a function to recombine the results.

sum_expand(tape[, group])

Splits a quantum tape measuring a Sum expectation into multiple tapes of summand expectations, and provides a function to recombine the results.

This transform accepts a single tape and returns a single tape:

convert_to_numpy_parameters(circuit)

Transforms a circuit to one with purely numpy parameters.

Decorators and utility functions

The following decorators and convenience functions are provided to help build custom QNode, quantum function, and tape transforms:

single_tape_transform(transform_fn)

For registering a tape transform that takes a tape and outputs a single new tape.

batch_transform(*args, **kwargs)

Class for registering a tape transform that takes a tape, and outputs a batch of tapes to be independently executed on a quantum device.

qfunc_transform(tape_transform)

Given a function which defines a tape transform, convert the function into one that applies the tape transform to quantum functions (qfuncs).

op_transform(*args, **kwargs)

Convert a function that applies to operators into a functional transform.

make_tape(fn)

Returns a function that generates the tape from a quantum function without any operation queuing taking place.

map_batch_transform(transform, tapes)

Map a batch transform over multiple tapes.

create_expand_fn(depth[, stop_at, device, …])

Create a function for expanding a tape to a given depth, and with a specific stopping criterion.

create_decomp_expand_fn(custom_decomps, dev)

Creates a custom expansion function for a device that applies a set of specified custom decompositions.

expand_invalid_trainable(tape[, depth])

Expand out a tape so that it supports differentiation of requested operations.

expand_invalid_trainable_hadamard_gradient(tape)

Expand out a tape so that it supports differentiation of requested operations with the Hadamard test gradient.

expand_multipar(tape[, depth])

Expand out a tape so that all its parametrized operations have a single parameter.

expand_trainable_multipar(tape[, depth])

Expand out a tape so that all its trainable operations have a single parameter.

expand_nonunitary_gen(tape[, depth])

Expand out a tape so that all its parametrized operations have a unitary generator.

Transforms for error mitigation

mitigate_with_zne(circuit, scale_factors, …)

Mitigate an input circuit using zero-noise extrapolation.

fold_global(circuit, scale_factor)

Differentiable circuit folding of the global unitary circuit.

poly_extrapolate(x, y, order)

Extrapolator to \(f(0)\) for polynomial fit.

richardson_extrapolate(x, y)

Polynomial fit where the degree of the polynomial is fixed to being equal to the length of x.

Transforms core

transform(quantum_transform[, …])

The transform function is to be used to validate and dispatch a quantum transform on PennyLane objects (tape, qfunc and Qnode).

transform_dispatcher

This module contains the transform function, the transform dispatcher and the transform container.