Loading [MathJax]/jax/output/HTML-CSS/jax.js

qml.fourier.circuit_spectrum

circuit_spectrum(tape, encoding_gates=None, decimals=8)[source]

Compute the frequency spectrum of the Fourier representation of simple quantum circuits ignoring classical preprocessing.

The circuit must only use simple single-parameter gates of the form eixjG as input-encoding gates, which allows the computation of the spectrum by inspecting the gates’ generators G. The most important example of such gates are Pauli rotations.

Note

More precisely, the circuit_spectrum function relies on the gate to define a generator, and will fail if gates marked as inputs do not have this attribute.

Gates are marked as input-encoding gates in the quantum function by giving them an id. If two gates have the same id, they are considered to be used to encode the same input xj. The encoding_gates argument can be used to indicate that only gates with a specific id should be interpreted as input-encoding gates. Otherwise, all gates with an explicit id are considered to be input-encoding gates.

Note

If no input-encoding gates are found, an empty dictionary is returned.

Parameters
  • tape (QNode or QuantumTape or Callable) – a quantum circuit in which input-encoding gates are marked by their id attribute

  • encoding_gates (list[str]) – list of input-encoding gate id strings for which to compute the frequency spectra

  • decimals (int) – number of decimals to which to round frequencies.

Returns

The transformed circuit as described in qml.transform. Executing this circuit will return a dictionary with the input-encoding gate id as keys and their frequency spectra as values.

Return type

qnode (QNode) or quantum function (Callable) or tuple[List[QuantumTape], function]

Details

A circuit that returns an expectation value which depends on N scalar inputs xj can be interpreted as a function f:RNR. This function can always be expressed by a Fourier-type sum

ω1Ω1ωNΩNcω1,,ωNeix1ω1eixNωN

over the frequency spectra ΩjR, j=1,,N. Each spectrum has the property that 0Ωj, and the spectrum is symmetric (for every ωΩj we have that ωΩj). If all frequencies are integer-valued, the Fourier sum becomes a Fourier series.

As shown in Vidal and Theis (2019) and Schuld, Sweke and Meyer (2020), if an input xj,j=1N, only enters into single-parameter gates of the form eixjG (where G is a Hermitian generator), the frequency spectrum Ωj is fully determined by the eigenvalues of G. In many situations, the spectra are limited to a few frequencies only, which in turn limits the function class that the circuit can express.

The circuit_spectrum function computes all frequencies that will potentially appear in the sets Ω1 to ΩN.

Example

Consider the following example, which uses non-trainable inputs x and trainable parameters w as arguments to the qnode.

import pennylane as qml
import numpy as np

n_layers = 2
n_qubits = 3
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def circuit(x, w):
    for l in range(n_layers):
        for i in range(n_qubits):
            qml.RX(x[i], wires=i, id="x"+str(i))
            qml.Rot(w[l,i,0], w[l,i,1], w[l,i,2], wires=i)
    qml.RZ(x[0], wires=0, id="x0")
    return qml.expval(qml.Z(0))

x = np.array([1, 2, 3])
w = np.random.random((n_layers, n_qubits, 3))
res = qml.fourier.circuit_spectrum(circuit)(x, w)
>>> print(qml.draw(circuit)(x, w))
0: ──RX(1.00,"x0")──Rot(0.03,0.03,0.37)──RX(1.00,"x0")──Rot(0.35,0.89,0.29)──RZ(1.00,"x0")─┤  <Z>
1: ──RX(2.00,"x1")──Rot(0.70,0.12,0.60)──RX(2.00,"x1")──Rot(0.04,0.03,0.88)────────────────┤
2: ──RX(3.00,"x2")──Rot(0.65,0.87,0.05)──RX(3.00,"x2")──Rot(0.37,0.53,0.02)────────────────┤
>>> for inp, freqs in res.items():
>>>     print(f"{inp}: {freqs}")
'x0': [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]
'x1': [-2.0, -1.0, 0.0, 1.0, 2.0]
'x2': [-2.0, -1.0, 0.0, 1.0, 2.0]

Note

While the Fourier spectrum usually does not depend on trainable circuit parameters or the actual values of the inputs, it may still change based on inputs to the QNode that alter the architecture of the circuit.

The input-encoding gates to consider can also be explicitly selected by using the encoding_gates keyword argument:

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x[0], wires=0, id="x0")
    qml.PhaseShift(x[0], wires=0, id="x0")
    qml.RX(x[1], wires=0, id="x1")
    return qml.expval(qml.Z(0))

x = np.array([1, 2])
res = qml.fourier.circuit_spectrum(circuit, encoding_gates=["x0"])(x)
>>> for inp, freqs in res.items():
>>>     print(f"{inp}: {freqs}")
'x0': [-2.0, -1.0, 0.0, 1.0, 2.0]

Note

The circuit_spectrum function does not check if the result of the circuit is an expectation, or if gates with the same id take the same value in a given call of the function.

The circuit_spectrum function works in all interfaces:

import tensorflow as tf

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev, interface='tf')
def circuit(x):
    qml.RX(x[0], wires=0, id="x0")
    qml.PhaseShift(x[1], wires=0, id="x1")
    return qml.expval(qml.Z(0))

x = tf.constant([1, 2])
res = qml.fourier.circuit_spectrum(circuit)(x)
>>> for inp, freqs in res.items():
>>>     print(f"{inp}: {freqs}")
'x0': [-1.0, 0.0, 1.0]
'x1': [-1.0, 0.0, 1.0]