qml.ops.rs_decomposition

rs_decomposition(op, epsilon, *, max_search_trials=20, max_factoring_trials=1000)[source]

Approximate a phase shift rotation gate in the Clifford+T basis using the Ross-Selinger algorithm.

This method implements the Ross-Selinger decomposition algorithm that approximates any arbitrary phase shift rotation gate with \(\epsilon > 0\) error. The procedure exits when the approximation error becomes less than \(\epsilon\), or when max_search_trials attempts have been made for solution search. In the latter case, the approximation error could be \(\geq \epsilon\).

This algorithm produces a decomposition with \(O(3\text{log}_2(1/\epsilon)) + O(\text{log}_2(\text{log}_2(1/\epsilon)))\) operations.

Note

Currently, the global phase \(\theta\) returned by the decomposition might differ from the true global phase \(\theta^{*}\) by an additive factor of \(\pi\).

Parameters:
Keyword Arguments:
  • max_search_trials (int) – The maximum number of attempts to find a solution while performing the grid search according to the Algorithm 7.6.1, in the arXiv:1403.2975v3. Default is 20.

  • max_factoring_trials (int) – The maximum number of attempts to find a prime factor while performing the factoring to solve the Diophantine equation (Algorithm 7.6.2b) for the solution found in the grid search. Default is 1000.

Returns:

A list of gates in the Clifford+T basis set that approximates the given operation along with a final global phase operation. The operations are in the circuit-order.

Return type:

list[Operation]

Raises:

ValueError – If the given operator is not a RZ or PhaseShift gate.

Example

Suppose one would like to decompose RZ with rotation angle \(\phi = \pi/3\):

import numpy as np
import pennylane as qml

op  = qml.RZ(np.pi/3, wires=0)
ops = qml.ops.rs_decomposition(op, epsilon=1e-3)

# Get the approximate matrix from the ops
matrix_rs = qml.prod(*reversed(ops)).matrix()

When the function is run for a sufficient max_search_trials, the output gate sequence should implement the same operation approximately, up to a global phase.

>>> qml.math.allclose(op.matrix(), matrix_rs, atol=1e-3)
True