map(template, observables, device, measure='expval', interface='autograd', diff_method='best', **kwargs)[source]

Map a quantum template over a list of observables to create a QNodeCollection.

The number of QNodes within the created QNode collection will match the number of observables passed. The device and the measurement type will either be applied to all QNodes in the collection, or can be provided as a list for more fine-grained control.

  • template (callable) –

    the ansatz for the circuit before the final measurement step Note that the template must have the following signature:

    template(params, wires, **kwargs)

    where params are the trainable weights of the variational circuit, wires is a list of integers representing the wires of the device, and kwargs are any additional keyword arguments that need to be passed to the template.

  • observables (Iterable[Observable]) – observables to measure during the final step of each circuit

  • device (Device, Sequence[Device]) – Corresponding device(s) where the resulting QNodeCollection should be executed. This can either be a single device, or a list of devices of length len(observables).

  • measure (str, Union(List[str], Tuple[str])) – Measurement(s) to perform. Options include 'expval', 'var', and 'sample'. This can either be a single measurement type, in which case it is applied to all observables, or a list of measurements of length len(observables).

  • interface (str, None) – which interface to use for the returned QNode collection. This affects the types of objects that can be passed to/returned from the QNode. Supports all interfaces supported by the qnode() decorator.

  • diff_method (str, None) – the method of differentiation to use in the created QNodeCollection. Supports all differentiation methods supported by the qnode() decorator.


a collection of QNodes executing the circuit template with the specified measurements

Return type



Let’s define a custom two-wire template:

def my_template(params, wires, **kwargs):
    qml.RX(params[0], wires=wires[0])
    qml.RX(params[1], wires=wires[1])

We want to compute the expectation values over the following list of two-qubit observables:

>>> obs_list = [qml.PauliX(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.PauliX(1)]

This can be easily done with the map function:

>>> dev = qml.device("default.qubit", wires=2)
>>> qnodes = qml.map(my_template, obs_list, dev, measure="expval")

The returned QNodeCollection can be evaluated, returning the results from each mapped QNode as an array:

>>> params = [0.54, 0.12]
>>> qnodes(params)
array([-0.06154835  0.99280864])