qp.SumOfSlatersPrep

class SumOfSlatersPrep(coefficients, wires, indices, enumeration_wires=(), identification_wires=(), qrom_work_wires=(), mcx_cache_wires=())[source]

Bases: Operator2

Prepare an arbitrary quantum state with the sum-of-Slaters technique.

This operation prepares an arbitrary state

\[|\psi\rangle = \sum_{\ell \in L } c_\ell |\ell\rangle,\]

where \(L\) denotes the set of indices and \(c_\ell\) is the coefficient corresponding to the index \(\ell\in L\). The state \(|\ell\rangle\) is a computational basis state, interpreted via the binary representation of \(\ell\).

This state preparation technique was introduced in Sec. III A of Fomichev et al., PRX Quantum 5, 040339 and is tailored to sparse states.

See also

select_sos_rows() and compute_sos_encoding() for the required classical coprocessing, as well as PartialUnaryStatePreparation for another sparse state preparation technique.

Parameters:
  • coefficients (np.ndarray) – Coefficients of the sparse state to prepare. The ordering should match that in indices.

  • wires (WiresLike) – Wires on which to prepare the state.

  • enumeration_wires (WiresLike) – Work wires used for the enumeration register. For \(d\) entries in the state, \(\lceil \log_2 (d)\rceil\) qubits are required.

  • identification_wires (WiresLike) – Work wires used for the identification register. The required number of qubits depends on the particular indices of the sparse state, but it is at most \(2d-1\) for \(d\) entries in the state.

  • qrom_work_wires (WiresLike) – Work wires used by the QROM subroutine. For \(d\) entries in the state, \(\lceil \log_2 (d)\rceil - 1\) qubits are required.

  • mcx_cache_wires (WiresLike) – Work wires used for caching AND values when uncomputing the enumeration register with multicontrolled bit flips. The required number of qubits depends on the particular indices of the sparse state, but it is at most \(2d-2\) for \(d\) entries in the state.

  • indices (tuple[int] | None) – Indices of the sparse state to prepare. The ordering should match that in coefficients. None indicates unknown indices for the purposes of resource estimation.

The sizes for the numerous optional work wire registers can be computed with SumOfSlatersPrep.required_register_sizes(indices, wires).

Warning

Note that we require coefficients to be treated as numerical data in the form of an array, whereas the indices need to be hashable, and thus will be treated as static information. This is because indices significantly impacts the structure and size of the circuit that realizes the state preparation.

Example

Consider a sparse state specified by normalized coefficients and statevector indices pointing to the populated computational basis states:

import pennylane as qp
import numpy as np

coefficients = np.array([1, -1j, 1j, 1, 1, -1j, 1, 1j]) / np.sqrt(8)
indices = (0, 1, 2, 4, 8, 16, 32, 64)
wires = list(range(7))

This is all the information we require to create the state preparation: coefficients, indices, and wires. The indices correspond to the computational basis states interpreted via their binary representation (e.g., \(|3\rangle = |11\rangle\) for two qubits or \(|3\rangle = |011\rangle\) for three qubits).

qp.decomposition.enable_graph()

gate_set = {"QROM", "TemporaryAND", "Adjoint(TemporaryAND)", "MultiplexerStatePreparation", "CNOT", "X"}

first_free_wire = max(wires)+1

@qp.transforms.resolve_dynamic_wires(min_int=first_free_wire)
@qp.transforms._tape_decompose(gate_set=gate_set, num_work_wires=14)
@qp.qnode(qp.device("lightning.qubit", wires=21))
def circuit():
    qp.SumOfSlatersPrep(coefficients, wires, indices)
    return qp.state()

We can check that we prepared the right state:

>>> prepared_state = circuit()[::2**14] # Slice the state, as there are 14 work wires
>>> where = np.where(prepared_state)
>>> print(where)
(array([ 0,  1,  2,  4,  8, 16, 32, 64]),)
>>> # Adding 0.0 to the rounded result will prevent stochastic signed zeros
>>> print(np.round(prepared_state[where], 4) + 0.0)
[0.3536+0.j     0.    -0.3536j 0.    +0.3536j 0.3536+0.j
 0.3536+0.j     0.    -0.3536j 0.3536+0.j     0.    +0.3536j]

That looks exactly right! Internally, the state preparation looks like this:

>>> print(qp.draw(circuit, show_matrices=False, max_length=195)())
 0: ──────────────────────────────────╭QROM(M1)─╭●───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 1: ──────────────────────────────────├QROM(M1)─│────────╭●──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 2: ──────────────────────────────────├QROM(M1)─│──╭●────│──╭●───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 3: ──────────────────────────────────├QROM(M1)─│──│─────│──│─────╭●─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 4: ──────────────────────────────────├QROM(M1)─│──│──╭●─│──│──╭●─│──╭●──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 5: ──────────────────────────────────├QROM(M1)─│──│──│──│──│──│──│──│──╭●───────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 6: ──────────────────────────────────├QROM(M1)─│──│──│──│──│──│──│──│──│──╭●────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 7: ─╭MultiplexerStatePreparation(M0)─├◑────────│──│──│──│──│──│──│──│──│──│─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ···
 8: ─├MultiplexerStatePreparation(M0)─├◑────────│──│──│──│──│──│──│──│──│──│───────────────────────────────────────────────────╭X────────────────────────────────╭X──────────────────────────── ···
 9: ─╰MultiplexerStatePreparation(M0)─├◑────────│──│──│──│──│──│──│──│──│──│─────────────────╭X────────────────────────────────│─────────────────────────────────│──╭X───────────────────────── ···
10: ──────────────────────────────────│─────────╰X─╰X─╰X─│──│──│──│──│──│──│───X─╭●──────────│───────────────●╮────╭●──────────│───────────────●╮──X─╭●──────────│──│───────────────●╮──X─╭●─── ···
11: ──────────────────────────────────│──────────────────╰X─╰X─╰X─│──│──│──│───X─├●──────────│───────────────●┤────├●──────────│───────────────●┤──X─├●──────────│──│───────────────●┤──X─├●─── ···
12: ──────────────────────────────────│───────────────────────────╰X─╰X─│──│───X─│──╭●───────│───────────●╮───│────│──╭●───────│───────────●╮───│──X─│──╭●───────│──│───────────●╮───│────│──╭● ···
13: ──────────────────────────────────│─────────────────────────────────╰X─│───X─│──│──╭●────│───────●╮───│───│──X─│──│──╭●────│───────●╮───│───│──X─│──│──╭●────│──│───────●╮───│───│────│──│─ ···
14: ──────────────────────────────────│────────────────────────────────────╰X────│──│──│──╭●─│───●╮───│───│───│──X─│──│──│──╭●─│───●╮───│───│───│────│──│──│──╭●─│──│───●╮───│───│───│────│──│─ ···
15: ──────────────────────────────────├work──────────────────────────────────────│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│─ ···
16: ──────────────────────────────────╰work──────────────────────────────────────│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│─ ···
17: ─────────────────────────────────────────────────────────────────────────────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────╰⊕─├● ···
18: ────────────────────────────────────────────────────────────────────────────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯───────────╰⊕ ···
19: ───────────────────────────────────────────────────────────────────────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯───────────────── ···
20: ──────────────────────────────────────────────────────────────────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯───────────────────── ···

 0: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╭●────────────────────────────┤ ╭State
 1: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│────────╭●───────────────────┤ ├State
 2: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│──╭●────│──╭●────────────────┤ ├State
 3: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│──│─────│──│─────╭●──────────┤ ├State
 4: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│──│──╭●─│──│──╭●─│──╭●───────┤ ├State
 5: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│──│──│──│──│──│──│──│──╭●────┤ ├State
 6: ··· ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────│──│──│──│──│──│──│──│──│──╭●─┤ ├State
 7: ··· ───────╭X────────────────────────────────╭X───────────────────────────────────╭X───────────────────────────────────╭X───────────────────────│──│──│──│──│──│──│──│──│──│──┤ ├State
 8: ··· ───────│─────────────────────────────────│────────────────────────────────────│──╭X────────────────────────────────│──╭X────────────────────│──│──│──│──│──│──│──│──│──│──┤ ├State
 9: ··· ───────│─────────────────────────────────│──╭X────────────────────────────────│──│─────────────────────────────────│──│──╭X─────────────────│──│──│──│──│──│──│──│──│──│──┤ ├State
10: ··· ───────│───────────────●╮──X─╭●──────────│──│───────────────●╮──X─╭●──────────│──│───────────────●╮──X─╭●──────────│──│──│───────────────●╮─╰X─╰X─╰X─│──│──│──│──│──│──│──┤ ├State
11: ··· ───────│───────────────●┤──X─├●──────────│──│───────────────●┤────├●──────────│──│───────────────●┤──X─├●──────────│──│──│───────────────●┤──X───────╰X─╰X─╰X─│──│──│──│──┤ ├State
12: ··· ───────│───────────●╮───│──X─│──╭●───────│──│───────────●╮───│────│──╭●───────│──│───────────●╮───│────│──╭●───────│──│──│───────────●╮───│──X────────────────╰X─╰X─│──│──┤ ├State
13: ··· ─╭●────│───────●╮───│───│────│──│──╭●────│──│───────●╮───│───│────│──│──╭●────│──│───────●╮───│───│────│──│──╭●────│──│──│───────●╮───│───│──X──────────────────────╰X─│──┤ ├State
14: ··· ─│──╭●─│───●╮───│───│───│────│──│──│──╭●─│──│───●╮───│───│───│────│──│──│──╭●─│──│───●╮───│───│───│────│──│──│──╭●─│──│──│───●╮───│───│───│──X─────────────────────────╰X─┤ ├State
15: ··· ─│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──│────│───│───│───│───────────────────────────────┤ ├State
16: ··· ─│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──│────│───│───│───│───────────────────────────────┤ ├State
17: ··· ─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│──│────│───│──●┤──⊕╯───────────────────────────────┤ ├State
18: ··· ─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│──│────│──●┤──⊕╯───────────────────────────────────┤ ├State
19: ··· ─╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯──────────────────╰⊕─├●─│──│──│───●┤──⊕╯───────────────────────────────────────┤ ├State
20: ··· ────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●─╰●──⊕╯───────────────────────────────────────────┤ ╰State

Here, the first seven wires (0 to 6) are the target wires of the state preparation, wires 7, 8, 9 form the enumeration register, the next five wires (10 to 14) are the encoding register, and the pair of wires 15, 16 as well as the wires 17 to 20 are work wires for the QROM and the enumeration uncomputation, respectively.

Resources are general, for \(D \geq 2\) entries in indices, \(d = \lceil \log_2 D \rceil\) enumeration wires and \(m\) encoding qubits (the number of rows of the encoding computed by compute_sos_encoding()). Each circuit shows one example of the rule. Use a rule when decomposing with qp.decompose:

qp.decompose(gate_set=..., fixed_decomps={qp.SumOfSlatersPrep: "<name>"})

_sos_state_prep

_sos_state_prep

Example: indices=(0, 1, 2, 4) on three wires, \(D=4\), \(m=3\), \(u=0\)

Gate

Count

Example

MultiplexerStatePreparation on \(d\) wires

\(1\)

1

QROM

\(1\)

1

CNOT

\(2u + \sum_{k=1}^{D-1} \mathrm{wt}(k)\)

4

TemporaryAND

\((D-1)(m-1)\)

6

Adjoint(TemporaryAND)

\((D-1)(m-1)\)

6

X

\(N_X \leq D m\)

8

where \(u\) is the number of ones in the encoding matrix (\(u=0\) for an identity encoding), \(\mathrm{wt}(k)\) the number of ones in the binary representation of \(k\), and \(N_X\) the number of bit flips between consecutive identification bit strings (data dependent)

Reduced circuit complexity for identity encodings

Depending on the indices passed to the state preparation, they may or may not be reducible to short enough sub-bitstrings such that no further encoding is required. In this case, the blocks of CNOT gates after the QROM and at the end, as seen in the example above, are not needed. For example, consider the following modification of the example:

coefficients = np.array([0.25, 0.25j, -0.25, 0.5, 0.5, 0.25, -0.25j, 0.25, -0.25, 0.25])
indices = (0, 1, 4, 13, 14, 17, 19, 22, 23, 25)
wires = list(range(5))
first_free_wire = max(wires)+1

@qp.transforms.resolve_dynamic_wires(min_int=first_free_wire)
@qp.transforms._tape_decompose(gate_set=gate_set, num_work_wires=11)
@qp.qnode(qp.device("lightning.qubit", wires=16))
def circuit():
    qp.SumOfSlatersPrep(coefficients, wires, indices)
    return qp.state()

In this case, we only require eight work wires, because the encoding blocks can be skipped.

>>> prepared_state = circuit()[::2**11] # Slice the state, as there are eleven work wires
>>> where = np.where(np.abs(prepared_state)>1e-12)
>>> print(where)
(array([ 0,  1,  4, 13, 14, 17, 19, 22, 23, 25]),)
>>> # Adding 0.0 to the rounded result will prevent stochastic signed zeros
>>> print(np.round(prepared_state[where], 4) + 0.0)
[ 0.25+0.j    0.  +0.25j -0.25+0.j    0.5 +0.j    0.5 +0.j    0.25+0.j
  0.  -0.25j  0.25+0.j   -0.25+0.j    0.25+0.j  ]

The reduced circuit looks like this:

>>> print(qp.draw(circuit, show_matrices=False, max_length=205)())
 0: ──────────────────────────────────╭QROM(M1)──X─╭●──────────────────────────●╮────╭●──────────────────────────●╮────╭●─────────────────────────────●╮────╭●──────────────────────────●╮──X─╭●───────── ···
 1: ──────────────────────────────────├QROM(M1)──X─├●──────────────────────────●┤────├●──────────────────────────●┤──X─├●─────────────────────────────●┤────├●──────────────────────────●┤──X─├●───────── ···
 2: ──────────────────────────────────├QROM(M1)──X─│──╭●───────────────────●╮───│──X─│──╭●───────────────────●╮───│────│──╭●──────────────────────●╮───│────│──╭●───────────────────●╮───│──X─│──╭●────── ···
 3: ──────────────────────────────────├QROM(M1)──X─│──│──╭●────────────●╮───│───│────│──│──╭●────────────●╮───│───│────│──│──╭●───────────────●╮───│───│──X─│──│──╭●────────────●╮───│───│──X─│──│──╭●─── ···
 4: ──────────────────────────────────├QROM(M1)────│──│──│──╭●─────●╮───│───│───│──X─│──│──│──╭●─────●╮───│───│───│──X─│──│──│──╭●────────●╮───│───│───│──X─│──│──│──╭●─────●╮───│───│───│──X─│──│──│──╭● ···
 5: ─╭MultiplexerStatePreparation(M0)─├◑───────────│──│──│──│───────│───│───│───│────│──│──│──│───────│───│───│───│────│──│──│──│──────────│───│───│───│────│──│──│──│───────│───│───│───│────│──│──│──│─ ···
 6: ─├MultiplexerStatePreparation(M0)─├◑───────────│──│──│──│───────│───│───│───│────│──│──│──│───────│───│───│───│────│──│──│──│──────────│───│───│───│────│──│──│──│──╭X───│───│───│───│────│──│──│──│─ ···
 7: ─├MultiplexerStatePreparation(M0)─├◑───────────│──│──│──│───────│───│───│───│────│──│──│──│──╭X───│───│───│───│────│──│──│──│──╭X──────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│─ ···
 8: ─╰MultiplexerStatePreparation(M0)─├◑───────────│──│──│──│──╭X───│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──╭X───│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│─ ···
 9: ──────────────────────────────────├work────────│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│─ ···
10: ──────────────────────────────────├work────────│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│─ ···
11: ──────────────────────────────────╰work────────│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│─ ···
12: ───────────────────────────────────────────────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│─ ···
13: ──────────────────────────────────────────────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│─ ···
14: ─────────────────────────────────────────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯──────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├● ···
15: ────────────────────────────────────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕ ···

 0: ··· ────────────────────●╮────╭●─────────────────────────────●╮────╭●────────────────────────────────●╮────╭●──────────────────────────●╮────╭●─────────────────────────────●╮────┤ ╭State
 1: ··· ────────────────────●┤────├●─────────────────────────────●┤────├●────────────────────────────────●┤────├●──────────────────────────●┤──X─├●─────────────────────────────●┤────┤ ├State
 2: ··· ────────────────●╮───│────│──╭●──────────────────────●╮───│──X─│──╭●─────────────────────────●╮───│────│──╭●───────────────────●╮───│──X─│──╭●──────────────────────●╮───│──X─┤ ├State
 3: ··· ────────────●╮───│───│──X─│──│──╭●───────────────●╮───│───│────│──│──╭●──────────────────●╮───│───│────│──│──╭●────────────●╮───│───│──X─│──│──╭●───────────────●╮───│───│──X─┤ ├State
 4: ··· ────────●╮───│───│───│────│──│──│──╭●────────●╮───│───│───│──X─│──│──│──╭●───────────●╮───│───│───│──X─│──│──│──╭●─────●╮───│───│───│────│──│──│──╭●────────●╮───│───│───│────┤ ├State
 5: ··· ─────────│───│───│───│────│──│──│──│──────────│───│───│───│────│──│──│──│─────────────│───│───│───│────│──│──│──│──╭X───│───│───│───│────│──│──│──│──╭X──────│───│───│───│────┤ ├State
 6: ··· ─╭X──────│───│───│───│────│──│──│──│──╭X──────│───│───│───│────│──│──│──│──╭X─────────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│───────│───│───│───│────┤ ├State
 7: ··· ─│───────│───│───│───│────│──│──│──│──│──╭X───│───│───│───│────│──│──│──│──│──╭X──────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│───────│───│───│───│────┤ ├State
 8: ··· ─│──╭X───│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──╭X───│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──╭X───│───│───│───│────┤ ├State
 9: ··· ─│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────┤ ├State
10: ··· ─│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────┤ ├State
11: ··· ─│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────│──│──│──│──│──│──│────│───│───│───│────│──│──│──│──│────│───│───│───│────│──│──│──│──│──│────│───│───│───│────┤ ├State
12: ··· ─│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│────│───│──●┤──⊕╯────╰⊕─├●─│──│──│──│────│───│──●┤──⊕╯────┤ ├State
13: ··· ─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│────│──●┤──⊕╯───────────╰⊕─├●─│──│──│────│──●┤──⊕╯────────┤ ├State
14: ··· ─│──│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯──────────────────╰⊕─├●─│──│──│───●┤──⊕╯──────────────────╰⊕─├●─│───●┤──⊕╯──────────────────╰⊕─├●─│──│───●┤──⊕╯────────────┤ ├State
15: ··· ─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●─╰●──⊕╯─────────────────────────╰⊕─╰●──⊕╯─────────────────────────╰⊕─╰●─╰●──⊕╯────────────────┤ ╰State

As we can see, the ladders of elbow, or TemporaryAND gates are now controlled on the target register directly, rather than the encoding register, which we thus can skip for the identity encoding.

Dynamic work wires

Note that in the example above, wires with labels 5 to 15 were dynamically allocated. These registers can optionally be provided explicitly. If they are not, they will be dynamically allocated. We can see an initial dense state preparation via StatePrep on fewer qubits (depicted as |Ψ⟩ on the first four dynamic wires in the above diagram), a QROM and a sequence of elbow ladders that set a caching qubit (qubit index 15), which then controls CNOT gates that perform the actual uncomputation.

Note that we guessed the required number of work wires (num_work_wires) in decompose() and employed resolve_dynamic_wires() to assign integer wire labels to those dynamically allocated wires. If we want to know the required wire register sizes ahead of time, they can be computed with SumOfSlatersPrep.required_register_sizes:

>>> sizes = qp.SumOfSlatersPrep.required_register_sizes(indices, len(wires))
>>> print(sizes)
{'wires': 5,
 'enumeration_wires': 4,
 'identification_wires': 0,
 'qrom_work_wires': 3,
 'mcx_cache_wires': 4}

Note that work wires of subroutines like QROM or the elbow ladders that realize a MultiControlledX gate are accounted for explicitly. Even thought the decomposition of SumOfSlatersPrep allocates the needed work wires automatically, you may want to allocate them explicitly. Generically, this can be done with registers():

>>> print(qp.registers(sizes))
{'wires': Wires([0, 1, 2, 3, 4]),
 'enumeration_wires': Wires([5, 6, 7, 8]),
 'identification_wires': Wires([]),
 'qrom_work_wires': Wires([9, 10, 11]),
 'mcx_cache_wires': Wires([12, 13, 14, 15])}

arg_specs

The expected types for the arguments of an operator.

arguments

Dictionary mapping argument names to their values.

arithmetic_depth

Arithmetic depth of the operator.

batch_size

Batch size of the operator if it is used with broadcasted parameters.

coefficients

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

compilable_argnames

The names of arguments that are treated as compilable static arguments.

compilable_args

Dictionary mapping compilable argument names to their values.

data

Legacy Operator compatibility view of dynamic numerical arguments.

dynamic_argnames

The names of arguments that are treated as dynamic.

dynamic_args

Dictionary mapping dynamic argument names to their values.

enumeration_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

grad_method

Gradient computation method.

grad_recipe

Compute 'grad_recipe' lazily.

has_adjoint

has_decomposition

has_diagonalizing_gates

has_fixed_sig

Whether the expected signature of an operator is fixed.

has_generator

has_matrix

has_sparse_matrix

hybrid_argnames

The names of arguments that represent dynamic data wrapped in static structures (known as Pytrees).

hybrid_args

Dictionary mapping hybrid argument names to their values.

hyperparameters

Legacy Operator compatibility view of non-dynamic, non-wire constructor arguments.

identification_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

indices

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

is_fully_abstract

Whether this operator contains only abstract data.

is_verified_hermitian

This property determines if an operator is verified to be Hermitian.

mcx_cache_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

name

Operator name.

ndim_params

Number of dimensions per trainable parameter of the operator.

num_params

Number of trainable parameters.

parameters

Legacy Operator compatibility view of dynamic numerical arguments as a list.

pauli_rep

A PauliSentence representation of the Operator, or None if it doesn't have one.

qrom_work_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

static_argnames

The names of arguments that are treated as static.

static_args

Dictionary mapping static argument names to their values.

wire_argnames

The names of arguments corresponding to wires.

wire_args

Dictionary mapping wire argument names to their values.

wire_sizes

The expected number of wire labels for each wire argument.

wires

Wires that the operator acts on.

arg_specs = {'wires': AbstractWires(-1)}

The expected types for the arguments of an operator. This attribute is optional—not setting it has no loss of functionality. If set, it can be used to perform automatic validation of an operators inputs during construction. Additionally, when defining decomposition rules for an operator, operator types with arg_specs that spans all the arguments with static types can be placed in the rules’ resources without needing to fully construct abstract operators.

Note

A type that is listed in ‘arg_specs’ says what an argument is allowed to be, not what it actually is. For example, if arg_specs contains Complex[-1, -1], the Operator can still be instantiated with a real float64 array, which will then be reported as complex even though it holds real data.

The decomposition graph goes by the reported type, so real and complex inputs will look like the same operator and share one rule. To let them decompose differently, leave the argument out of arg_specs and give each rule a register_condition that checks the type. For a concrete example see BasisRotation.

arguments

Dictionary mapping argument names to their values.

arithmetic_depth

Arithmetic depth of the operator.

batch_size

Batch size of the operator if it is used with broadcasted parameters.

The batch_size is determined based on ndim_params and the provided parameters for the operator. If (some of) the latter have an additional dimension, and this dimension has the same size for all parameters, its size is the batch size of the operator. If no parameter has an additional dimension, the batch size is None.

Returns:

Size of the parameter broadcasting dimension if present, else None.

Return type:

int or None

coefficients

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

compilable_argnames = ('indices',)

The names of arguments that are treated as compilable static arguments. Compilable static arguments are a subset of static arguments—arguments whose concrete values are known when compiling the program. But, unlike static_argnames, they are lowered to the compiler intermediate representation, making their values visible to the compiler, which may be useful if the compiler needs to interact with such values. Such values may include numbers, strings, and lists, tuples, or dictionaries thereof. This feature is opt-in; if any static arguments are not guaranteed to be compilable, it is safer to place them in static_argnames.

Note

An operator can only specify static_argnames and hybrid_argnames, or compilable_argnames, but not both; if any static or hybrid arguments are not or cannot be lowered to the IR, then all static and hybrid arguments are assumed to not be lowerable.

compilable_args

Dictionary mapping compilable argument names to their values.

data

Legacy Operator compatibility view of dynamic numerical arguments.

dynamic_argnames = ('coefficients',)

The names of arguments that are treated as dynamic. Dynamic arguments are numerical arguments whose concrete values may not be known at compile-time, such as rotation angles, matrices, etc., and may include trainable data. Additionally, dynamic arguments are the only ones used to infer batch_size, necessary for broadcasting support.

Inputs for these arguments must be scalars, arrays, or castable to arrays (e.g. multi- dimensional lists with homogenous shapes), and operator constructors will be responsible for “canonicalizing” them (such as casting a list input to an array). At compile-time, the concrete values of these arguments are assumed to be unknown—only their shapes and data types are known.

dynamic_args

Dictionary mapping dynamic argument names to their values.

enumeration_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

grad_method

Gradient computation method.

  • 'A': analytic differentiation using the parameter-shift method.

  • 'F': finite difference numerical differentiation.

  • None: the operation may not be differentiated.

Default is 'F', or None if the Operation has zero parameters.

grad_recipe

Compute ‘grad_recipe’ lazily.

has_adjoint = False
has_decomposition = True
has_diagonalizing_gates = False
has_fixed_sig = False

Whether the expected signature of an operator is fixed. If True, then the operator’s signature will always be fully known. When defining decomposition rules for an operator, operator types with fixed signatures can be placed in the rules’ resources without needing to fully construct abstract operators. This is set automatically when arg_specs covers every dynamic and wire argument, there are no hybrid, static, or compilable arguments, and every declared type is fully fixed (no unknown array shapes or wire counts).

has_generator = False
has_matrix = False
has_sparse_matrix = False
hybrid_argnames = ()

The names of arguments that represent dynamic data wrapped in static structures (known as Pytrees). Names in this category must be disjoint from dynamic_argnames, static_argnames, and compilable_argnames, but may overlap with wire_argnames when those arguments contain nested structures of wires. Examples of hybrid arguments include collections of wires or dynamic arrays, operators, etc.

Note

An operator can only specify static_argnames and hybrid_argnames, or compilable_argnames, but not both; if any static or hybrid arguments are not or cannot be lowered to the IR, then all static and hybrid arguments are assumed to not be lowerable.

hybrid_args

Dictionary mapping hybrid argument names to their values.

hyperparameters

Legacy Operator compatibility view of non-dynamic, non-wire constructor arguments.

identification_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

indices

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

is_fully_abstract

Whether this operator contains only abstract data.

is_verified_hermitian

This property determines if an operator is verified to be Hermitian.

Note

This property provides a fast, non-exhaustive check used for internal optimizations. It relies on quick, provable shortcuts (e.g., operator properties) rather than a full, computationally expensive check.

For a definitive check, use the pennylane.is_hermitian() function. Please note that this comes with increased computational cost.

Returns:

The property will return True if the operator is guaranteed to be Hermitian and False if the check is inconclusive and the operator may or may not be Hermitian.

Return type:

bool

Consider this operator,

>>> op = (qp.X(0) @ qp.Y(0) - qp.X(0) @ qp.Z(0)) * 1j

In this case, Hermicity cannot be verified and leads to an inconclusive result:

>>> op.is_verified_hermitian # inconclusive
False

However, using pennylane.is_hermitian() will give the correct answer:

>>> qp.is_hermitian(op) # definitive
True
mcx_cache_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

name

Operator name.

ndim_params

Number of dimensions per trainable parameter of the operator.

By default, this property returns the numbers of dimensions of the parameters used for the operator creation. If the parameter sizes for an operator subclass are fixed, this property can be overwritten to return the fixed value.

Returns:

Number of dimensions for each trainable parameter.

Return type:

tuple

num_params

Number of trainable parameters.

parameters

Legacy Operator compatibility view of dynamic numerical arguments as a list.

pauli_rep

A PauliSentence representation of the Operator, or None if it doesn’t have one.

qrom_work_wires

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

static_argnames = ()

The names of arguments that are treated as static. Static arguments are those whose concrete values are known when compiling the program. Arguments specified here are not lowered to a compiler intermediate representation (IR). Alternatively, if an argument is of a type that can be lowered, it can be moved to compilable_argnames.

Note

An operator can only specify static_argnames and hybrid_argnames, or compilable_argnames, but not both; if any static or hybrid arguments are not or cannot be lowered to the IR, then all static and hybrid arguments are assumed to not be lowerable.

static_args

Dictionary mapping static argument names to their values.

wire_argnames = ('wires', 'enumeration_wires', 'identification_wires', 'qrom_work_wires', 'mcx_cache_wires')

The names of arguments corresponding to wires. Values for these arguments are automatically wrapped in Wires objects by the Operator2 constructor. If an argument is expected to be a structure that wraps wires (known as pytrees), such as a list of wire registers, make sure to include its name in hybrid_argnames as well. Additionally, Operator2 does not descend into the pytree structure of such arguments, so subclasses must ensure every wire leaf inside a hybrid wire argument is already a Wires object before forwarding it to super().__init__.

The order in which names appear in wire_argnames determines the order in which their wires appear in op.wires (see Operator2.wires). For hybrid wire arguments, the contained Wires leaves are ordered by pytree traversal order. Wires contributed by arguments which are themselves Operator2 objects are appended after all wire_argnames wires. The special names "work_wires" and "work_wire" may be included in wire_argnames but their values are excluded from op.wires.

wire_args

Dictionary mapping wire argument names to their values.

wire_sizes = (None, None, None, None, None)

The expected number of wire labels for each wire argument. If any wire arguments support an arbitrary size, None must be used. By default, all wire arguments are assumed to support arbitrary sizes. For hybrid wire arguments, the wire size must always be None. Specifying wire sizes allows for additional automatic validation to be implemented, but, specifying it is optional if such validation is not needed.

wires

Wires that the operator acts on.

By default, the returned Wires are collected from the operator’s arguments in the following order:

  1. For each name in wire_argnames (in declaration order):

    • If the name is not in hybrid_argnames, the canonical value of that argument is added.

    • If the name is in hybrid_argnames, every Wires leaf of the argument’s pytree is added in pytree traversal order.

  2. After all wire_argnames have been processed, for each name in hybrid_argnames that is not in wire_argnames, the wires of any Operator2 leaves inside that argument are appended to the end in pytree traversal order.

Duplicate wires are removed while preserving the first occurrence, so the final result is the ordered union of all wires above.

Note

By default, work wires are not included in op.wires. In particular, wire arguments named work_wires or work_wire are excluded.

Note

This property may be overridden by developers if the default behaviour is not desired, for reasons such as including work wires or changing the order of the wires.

Returns:

wires

Return type:

Wires

adjoint()

Create an operation that is the adjoint of this one.

compute_decomposition(*args, **kwargs)

Representation of the operator as a product of other operators (static method).

compute_diagonalizing_gates(*args, **kwargs)

Sequence of gates that diagonalize the operator in the computational basis (static method).

compute_eigvals(*args, **kwargs)

Eigenvalues of the operator in the computational basis (static method).

compute_matrix(*args, **kwargs)

Representation of the operator as a canonical matrix in the computational basis (static method).

compute_sparse_matrix(*args[, format])

Representation of the operator as a sparse matrix in the computational basis (static method).

decomposition()

Representation of the operator as a product of other operators.

diagonalizing_gates()

Sequence of gates that diagonalize the operator in the computational basis.

eigvals()

Eigenvalues of the operator in the computational basis.

generator()

Generator of an operator that is in single-parameter-form.

label([decimals, base_label, cache])

A customizable string representation of the operator.

map_wires(wire_map)

Returns a new operator with its wires changed according to the given wire map.

matrix([wire_order])

Representation of the operator as a matrix in the computational basis.

pow(z)

A list of new operators equal to this one raised to the given power.

queue([context])

Append the operator to the Operator queue.

required_register_sizes(indices, num_wires)

Compute the register sizes required for SumOfSlatersPrep, for given computational basis states, indices, and number of target wires, num_wires.

simplify()

Reduce the depth of nested operators to the minimum.

sparse_matrix([wire_order, format])

Representation of the operator as a sparse matrix in the computational basis.

terms()

Representation of the operator as a linear combination of other operators.

adjoint()

Create an operation that is the adjoint of this one. Used to simplify Adjoint operators constructed by adjoint(). TODO: [sc-120844] Fix docstring after Adjoint is added

Adjointed operations are the conjugated and transposed version of the original operation. Adjointed ops are equivalent to the inverted operation for unitary gates.

Operator2.adjoint can be optionally defined by Operator developers, while adjoint() is the entry point for constructing generic adjoint representations.

Returns:

The adjointed operation.

>>> from pennylane.core.operator import Operator2
>>> class MyClass(Operator2):
...     dynamic_argnames = ("phi",)
...
...     def __init__(self, phi, wires):
...         super().__init__(phi, wires)
...
...     def adjoint(self):
...         return self
...
>>> op = MyClass(0.5, wires=0).adjoint()
>>> op
MyClass(0.5, wires=[0])
classmethod compute_decomposition(*args, **kwargs)

Representation of the operator as a product of other operators (static method).

\[O = O_1 O_2 \dots O_n.\]

Note

Operations making up the decomposition should be queued within the compute_decomposition method.

See also

decomposition().

Parameters:
  • *args (tuple) – Positional arguments of the operator

  • **kwargs (dict) – Keyword arguments of the operator

Returns:

decomposition of the operator

Return type:

list[Operator2]

static compute_diagonalizing_gates(*args, **kwargs)

Sequence of gates that diagonalize the operator in the computational basis (static method).

Given the eigendecomposition \(O = U \Sigma U^{\dagger}\) where \(\Sigma\) is a diagonal matrix containing the eigenvalues, the sequence of diagonalizing gates implements the unitary \(U^{\dagger}\).

The diagonalizing gates rotate the state into the eigenbasis of the operator.

Parameters:
  • *args (tuple) – Positional arguments of the operator

  • **kwargs (dict) – Keyword arguments of the operator

Returns:

list of diagonalizing gates

Return type:

list[.Operator2]

static compute_eigvals(*args, **kwargs)

Eigenvalues of the operator in the computational basis (static method).

If diagonalizing_gates are specified and implement a unitary \(U^{\dagger}\), the operator can be reconstructed as

\[O = U \Sigma U^{\dagger},\]

where \(\Sigma\) is the diagonal matrix containing the eigenvalues.

Otherwise, no particular order for the eigenvalues is guaranteed.

Parameters:
  • *args (tuple) – Positional arguments of the operator

  • **kwargs (dict) – Keyword arguments of the operator

Returns:

eigenvalues

Return type:

tensor_like

static compute_matrix(*args, **kwargs)

Representation of the operator as a canonical matrix in the computational basis (static method).

The canonical matrix is the textbook matrix representation that does not consider wires. Implicitly, this assumes that the wires of the operator correspond to the global wire order.

Parameters:
  • *args (tuple) – Positional arguments of the operator

  • **kwargs (dict) – Keyword arguments of the operator

Returns:

matrix representation

Return type:

tensor_like

static compute_sparse_matrix(*args, format='csr', **kwargs)

Representation of the operator as a sparse matrix in the computational basis (static method).

The canonical matrix is the textbook matrix representation that does not consider wires. Implicitly, this assumes that the wires of the operator correspond to the global wire order.

See also

sparse_matrix()

Parameters:
  • *args (tuple) – Positional arguments of the operator

  • format (str) – format of the returned scipy sparse matrix, for example ‘csr’

  • **kwargs (dict) – Keyword arguments of the operator

Returns:

sparse matrix representation

Return type:

scipy.sparse._csr.csr_matrix

decomposition()

Representation of the operator as a product of other operators.

\[O = O_1 O_2 \dots O_n\]

A DecompositionUndefinedError is raised if no representation by decomposition is defined.

Returns:

decomposition of the operator

Return type:

list[Operator2]

diagonalizing_gates()

Sequence of gates that diagonalize the operator in the computational basis.

Given the eigendecomposition \(O = U \Sigma U^{\dagger}\) where \(\Sigma\) is a diagonal matrix containing the eigenvalues, the sequence of diagonalizing gates implements the unitary \(U^{\dagger}\).

The diagonalizing gates rotate the state into the eigenbasis of the operator.

A DiagGatesUndefinedError is raised if no representation by decomposition is defined.

Returns:

a list of operators

Return type:

list[.Operator2] or None

eigvals()

Eigenvalues of the operator in the computational basis.

If diagonalizing_gates are specified and implement a unitary \(U^{\dagger}\), the operator can be reconstructed as

\[O = U \Sigma U^{\dagger},\]

where \(\Sigma\) is the diagonal matrix containing the eigenvalues.

Otherwise, no particular order for the eigenvalues is guaranteed.

Note

When eigenvalues are not explicitly defined, they are computed automatically from the matrix representation. Currently, this computation is not differentiable.

A EigvalsUndefinedError is raised if the eigenvalues have not been defined and cannot be inferred from the matrix representation.

Returns:

eigenvalues

Return type:

tensor_like

generator()

Generator of an operator that is in single-parameter-form.

For example, for operator

\[U(\phi) = e^{i\phi (0.5 Y + Z\otimes X)}\]

we get the generator

>>> U.generator()
0.5 * Y(0) + Z(0) @ X(1)

The generator may also be provided in the form of a dense or sparse Hamiltonian (using LinearCombination and SparseHamiltonian respectively).

label(decimals=None, base_label=None, cache=None)

A customizable string representation of the operator.

Parameters:
  • decimals=None (int) – If None, no parameters are included. Else, specifies how to round the parameters.

  • base_label=None (str) – overwrite the non-parameter component of the label

  • cache=None (dict) – dictionary that carries information between label calls in the same drawing

Returns:

label to use in drawings

Return type:

str

Example:

>>> op = qp.RX(1.23456, wires=0)
>>> op.label()
'RX'
>>> op.label(base_label="my_label")
'my_label'
>>> op = qp.RX(1.23456, wires=0)
>>> op.label()
'RX'
>>> op.label(decimals=2)
'RX\n(1.23)'
>>> op.label(base_label="my_label")
'my_label'
>>> op.label(decimals=2, base_label="my_label")
'my_label\n(1.23)'

Note that by default, operator wires and static inputs are not included in the label. To override this behaviour, Operator2 subclasses must override the label method.

If the operation has a matrix-valued parameter and a cache dictionary is provided, unique matrices will be cached in the 'matrices' key list. The label will contain the index of the matrix in the 'matrices' list.

>>> op2 = qp.QubitUnitary(np.eye(2), wires=0)
>>> cache = {'matrices': []}
>>> op2.label(cache=cache)
'U\n(M0)'
>>> cache['matrices']
[array([[1., 0.],
       [0., 1.]])]
>>> op3 = qp.QubitUnitary(np.eye(4), wires=(0,1))
>>> op3.label(cache=cache)
'U\n(M1)'
>>> cache['matrices']
[array([[1., 0.],
       [0., 1.]]), array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])]
map_wires(wire_map)

Returns a new operator with its wires changed according to the given wire map.

Parameters:

wire_map (dict) – dictionary containing the old wires as keys and the new wires as values

Returns:

new operator

Return type:

.Operator2

matrix(wire_order=None)

Representation of the operator as a matrix in the computational basis.

If wire_order is provided, the numerical representation considers the position of the operator’s wires in the global wire order. Otherwise, the wire order defaults to the operator’s wires.

If the matrix depends on trainable parameters, the result will be cast in the same autodifferentiation framework as the parameters.

A MatrixUndefinedError is raised if the matrix representation has not been defined.

See also

compute_matrix()

Parameters:

wire_order (Iterable) – global wire order, must contain all wire labels from the operator’s wires

Returns:

matrix representation

Return type:

tensor_like

pow(z)

A list of new operators equal to this one raised to the given power. This method is used to simplify Pow instances created by pow() or op ** power. TODO: [sc-120843] Fix docstring after Pow is added

Operator2.pow can be optionally defined by Operator developers, while pow() or op ** power are the entry point for constructing generic powers to exponents.

Parameters:

z (float) – exponent for the operator

Returns:

list[Operator2]

>>> from pennylane.core.operator import Operator2
>>> class MyClass(Operator2):
...     dynamic_argnames = ("phi",)
...
...     def __init__(self, phi, wires):
...         super().__init__(phi, wires)
...
...     def pow(self, z):
...         return [MyClass(self.phi*z, self.wires)]
...
>>> MyClass(0.5, 0).pow(2)
[MyClass(1.0, wires=[0])]
queue(context=<class 'pennylane.core.queuing.QueuingManager'>)

Append the operator to the Operator queue.

static required_register_sizes(indices, num_wires)[source]

Compute the register sizes required for SumOfSlatersPrep, for given computational basis states, indices, and number of target wires, num_wires.

Parameters:
  • indices (tuple[int] or AbstractArray) – Indices of computational basis states of the sparse state to be prepared with SumOfSlatersPrep.

  • num_wires (int) – Number of target wires on which SumOfSlatersPrep will prepare the state.

Returns:

Required register size per register name. Includes the target wires of length num_wires with the label "wires", matching the call signature of SumOfSlatersPrep.

Return type:

dict[str, int]

This function supports an abstract input for indices, in which case an upper bound for the register sizes, across all possible sets of basis states of the given length, is returned:

>>> indices = qp.typing.Int[45]
>>> qp.SumOfSlatersPrep.required_register_sizes(indices, 18)
{'wires': 18,
 'enumeration_wires': 6,
 'identification_wires': 11,
 'qrom_work_wires': 5,
 'mcx_cache_wires': 10}
simplify()

Reduce the depth of nested operators to the minimum.

Returns:

simplified operator

Return type:

.Operator2

sparse_matrix(wire_order=None, format='csr')

Representation of the operator as a sparse matrix in the computational basis.

If wire_order is provided, the numerical representation considers the position of the operator’s wires in the global wire order. Otherwise, the wire order defaults to the operator’s wires.

A SparseMatrixUndefinedError is raised if the sparse matrix representation has not been defined.

Parameters:
  • wire_order (Iterable) – global wire order, must contain all wire labels from the operator’s wires

  • format (str) – format of the returned scipy sparse matrix, for example ‘csr’

Returns:

sparse matrix representation

Return type:

scipy.sparse._csr.csr_matrix

terms()

Representation of the operator as a linear combination of other operators.

\[O = \sum_i c_i O_i\]

A TermsUndefinedError is raised if no representation by terms is defined.

Returns:

list of coefficients \(c_i\) and list of operations \(O_i\)

Return type:

tuple[list[tensor_like or float], list[.Operator2]]

Contents

Using PennyLane

Release news

Development

API

Internals