qml.PCPhase¶
- class PCPhase(phi, dim, wires)[source]¶
Bases:
Operation
A projector-controlled phase gate.
This gate applies a complex phase \(e^{i\phi}\) to the first \(dim\) basis vectors of the input state while applying a complex phase \(e^{-i \phi}\) to the remaining basis vectors. For example, consider the 2-qubit case where
dim = 3
:\[\begin{split}\Pi(\phi) = \begin{bmatrix} e^{i\phi} & 0 & 0 & 0 \\ 0 & e^{i\phi} & 0 & 0 \\ 0 & 0 & e^{i\phi} & 0 \\ 0 & 0 & 0 & e^{-i\phi} \end{bmatrix}.\end{split}\]This can also be written as \(\Pi(\phi) = \exp(i\phi(2\Pi-\mathbb{I}_N))\), where \(N=2^n\) is the Hilbert space dimension for \(n\) qubits and \(\Pi\) is the diagonal projector with
dim
ones andN-dim
zeros.Details:
Number of wires: Any (the operation can act on any number of wires)
Number of parameters: 1
Number of dimensions per parameter: (0,)
- Parameters:
phi (float) – rotation angle \(\phi\)
dim (int) – the dimension of the subspace
wires (Iterable[int, str], Wires) – the wires the operation acts on
id (str or None) – String representing the operation (optional)
Example:
We can define a circuit using
PCPhase
as follows:>>> op_3 = qml.PCPhase(0.27, dim = 3, wires=range(3))
The resulting operation applies a complex phase \(e^{0.27i}\) to the first \(dim = 3\) basis vectors and \(e^{-0.27i}\) to the remaining basis vectors, as we can see from the diagonal of the matrix for this circuit.
>>> print(np.round(np.diag(qml.matrix(op_3)),2)) [0.96+0.27j 0.96+0.27j 0.96+0.27j 0.96-0.27j 0.96-0.27j 0.96-0.27j 0.96-0.27j 0.96-0.27j]
We can also choose a different
dim
value to apply the phase shift to a different set of basis vectors as follows:>>> op_7 = qml.PCPhase(1.23, dim=7, wires=[1, 2, 3]) >>> print(np.round(np.diag(qml.matrix(op_7)),2)) [0.33+0.94j 0.33+0.94j 0.33+0.94j 0.33+0.94j 0.33+0.94j 0.33+0.94j 0.33+0.94j 0.33-0.94j]
PCPhase
operations are decomposed into (multi-)controlledPhaseShift
operations which share the same control values on common control wires, and Pauli-X operations, possibly complemented by a global phase.>>> op_13 = qml.PCPhase(1.23, dim=13, wires=[1, 2, 3, 4]) >>> print(qml.draw(op_13.decomposition)()) 1: ──GlobalPhase(-1.23)─╭●─────────╭●───────────┤ 2: ──GlobalPhase(-1.23)─╰Rϕ(-2.46)─├●───────────┤ 3: ──GlobalPhase(-1.23)────────────├○───────────┤ 4: ──GlobalPhase(-1.23)──X─────────╰Rϕ(2.46)──X─┤
If
dim
is a power of two, a single (multi-controlled)PhaseShift
gate is sufficient:>>> op_16 = qml.PCPhase(1.23, dim=16, wires=range(6)) >>> print(qml.draw(op_16.decomposition, wire_order=range(6), show_all_wires=True)()) 0: ──GlobalPhase(1.23)────╭○───────────┤ 1: ──GlobalPhase(1.23)──X─╰Rϕ(2.46)──X─┤ 2: ──GlobalPhase(1.23)─────────────────┤ 3: ──GlobalPhase(1.23)─────────────────┤ 4: ──GlobalPhase(1.23)─────────────────┤ 5: ──GlobalPhase(1.23)─────────────────┤
Attributes
Arithmetic depth of the operator.
Batch size of the operator if it is used with broadcasted parameters.
Control wires of the operator.
Gradient recipe for the parameter-shift method.
Integer hash that uniquely represents the operator.
Dictionary of non-trainable variables that this operation depends on.
Custom string to label a specific operator instance.
This property determines if an operator is likely hermitian.
String for the name of the operator.
Number of dimensions per trainable parameter that the operator depends on.
Number of trainable parameters that the operator depends on.
Number of wires the operator acts on.
Trainable parameters that the operator depends on.
A
PauliSentence
representation of the Operator, orNone
if it doesn't have one.A dictionary containing the minimal information needed to compute a resource estimate of the operator's decomposition.
Wires that the operator acts on.
- arithmetic_depth¶
Arithmetic depth of the operator.
- basis = 'Z'¶
- batch_size¶
Batch size of the operator if it is used with broadcasted parameters.
The
batch_size
is determined based onndim_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 isNone
.- Returns:
Size of the parameter broadcasting dimension if present, else
None
.- Return type:
int or None
- control_wires¶
Control wires of the operator.
For operations that are not controlled, this is an empty
Wires
object of length0
.- Returns:
The control wires of the operation.
- Return type:
- grad_method = 'A'¶
- grad_recipe = None¶
Gradient recipe for the parameter-shift method.
This is a tuple with one nested list per operation parameter. For parameter \(\phi_k\), the nested list contains elements of the form \([c_i, a_i, s_i]\) where \(i\) is the index of the term, resulting in a gradient recipe of
\[\frac{\partial}{\partial\phi_k}f = \sum_{i} c_i f(a_i \phi_k + s_i).\]If
None
, the default gradient recipe containing the two terms \([c_0, a_0, s_0]=[1/2, 1, \pi/2]\) and \([c_1, a_1, s_1]=[-1/2, 1, -\pi/2]\) is assumed for every parameter.- Type:
tuple(Union(list[list[float]], None)) or None
- has_adjoint = True¶
- has_decomposition = True¶
- has_diagonalizing_gates = False¶
- has_generator = True¶
- has_matrix = True¶
- has_qfunc_decomposition = False¶
- has_sparse_matrix = False¶
- hash¶
Integer hash that uniquely represents the operator.
- Type:
int
- hyperparameters¶
Dictionary of non-trainable variables that this operation depends on.
- Type:
dict
- id¶
Custom string to label a specific operator instance.
- is_hermitian¶
This property determines if an operator is likely hermitian.
Note
It is recommended to use the
is_hermitian()
function. Although this function may be expensive to calculate, theop.is_hermitian
property can lead to technically incorrect results.If this property returns
True
, the operator is guaranteed to be hermitian, but if it returnsFalse
, the operator may still be hermitian.As an example, consider the following edge case:
>>> op = (qml.X(0) @ qml.Y(0) - qml.X(0) @ qml.Z(0)) * 1j >>> op.is_hermitian False
On the contrary, the
is_hermitian()
function will give the correct answer:>>> qml.is_hermitian(op) True
- name¶
String for the name of the operator.
- ndim_params = (0,)¶
Number of dimensions per trainable parameter that the operator depends on.
- Type:
tuple[int]
- num_params = 1¶
Number of trainable parameters that the operator depends on.
- Type:
int
- num_wires = None¶
Number of wires the operator acts on.
- parameter_frequencies = [(2,)]¶
- parameters¶
Trainable parameters that the operator depends on.
- pauli_rep¶
A
PauliSentence
representation of the Operator, orNone
if it doesn’t have one.
- resource_keys = {'dim', 'num_wires'}¶
- resource_params¶
Methods
adjoint
()Computes the adjoint of the operator.
compute_decomposition
(*params, wires, ...)Representation of the PCPhase operator as a product of other operators (static method).
compute_diagonalizing_gates
(*params, wires, ...)Sequence of gates that diagonalize the operator in the computational basis (static method).
compute_eigvals
(*params, **hyperparams)Get the eigvals for the Pi-controlled phase unitary.
compute_matrix
(phi, dimension)Get the matrix representation of Pi-controlled phase unitary.
compute_qfunc_decomposition
(*args, ...)Experimental method to compute the dynamic decomposition of the operator with program capture enabled.
compute_sparse_matrix
(*params[, format])Representation of the operator as a sparse matrix in the computational basis (static method).
Representation of the operator as a product of other operators.
Sequence of gates that diagonalize the operator in the computational basis.
eigvals
()Eigenvalues of the operator in the computational basis.
Generator of the
PCPhase
operator, which is in single-parameter-form.label
([decimals, base_label, cache])The label of the operator when displayed in a circuit.
map_wires
(wire_map)Returns a copy of the current 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)Computes the operator raised to z.
queue
([context])Append the operator to the Operator queue.
simplify
()Simplifies the operator if possible.
The parameters required to implement a single-qubit gate as an equivalent
Rot
gate, up to a global phase.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.
- static compute_decomposition(*params, wires, **hyperparams)[source]¶
Representation of the PCPhase operator as a product of other operators (static method).
- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parameters
attributewires (Iterable[Any], Wires) – wires that the operator acts on
**hyperparams (dict) – non-trainable hyper-parameters of the operator, as stored in the
hyperparameters
attribute
- Returns:
decomposition of the operator
- Return type:
list[Operator]
In short, this decomposition relies on decomposing the generator (see
generator()
) of thePCPhase
gate into generators of multicontrolledPhaseShift
gates, potentially complemented with (non-controlled) Pauli-X gates and/or a global phase. For example, fordim=13
on four qubits:>>> op_13 = qml.PCPhase(1.23, dim=13, wires=[1, 2, 3, 4]) >>> print(qml.draw(op_13.decomposition)()) 1: ──GlobalPhase(-1.23)─╭●─────────╭●───────────┤ 2: ──GlobalPhase(-1.23)─╰Rϕ(-2.46)─├●───────────┤ 3: ──GlobalPhase(-1.23)────────────├○───────────┤ 4: ──GlobalPhase(-1.23)──X─────────╰Rϕ(2.46)──X─┤
In the following we provide a detailed example for illustration purposes.
Detailed example
Consider the projector-controlled phase gate on \(n=4\) qubits and with \(d=\texttt{dim}=3\), i.e,
>>> op_3 = qml.PCPhase(1.23, dim=3, wires=[0, 1, 2, 3])
It acts on \(N=2^n=16\)-dimensional vectors and is described by
\[\Pi(\phi) = \exp(i\phi G) = \exp(i\phi(2\Pi-\mathbb{I}_N)),\]where \(G\) is a diagonal matrix with \(d=3\) ones, followed by \(2^n-d = 16 - 3=13\) negative ones. Accordingly, \(\Pi\) is diagonal with \(3\) ones and \(13\) zeros.
First, we implement the global phase generated by \(\mathbb{I}_N\) with a
GlobalPhase
gate with angle \(-\phi\). Then we decompose \(d\) into powers of two with positive or negative sign, via \(d=3=4-1 = 2^2-2^0\). This decomposition tells us that we can write the target gate with two (multi-)controlled phase shift gates. For this, we rewrite the projector \(\Pi\) according to the decomposition as\[\begin{split}\Pi &= \text{diag}(1, 1, 1, 0, 0, \dots, 0)\\ &=\text{diag}(1, 1, 1, 1, 0, \dots, 0) -\text{diag}(0, 0, 0, 1, 0, \dots, 0)\end{split}\]where \(0,\dots, 0\) indicates \(12\) zeros each time. How do we realize this projector decomposition on the gate level?
A singly-controlled phase shift gate applies a phase to a quarter of all computational basis states (the control filters by the state of one qubit, and the phase shift gate itself filters by the \(|1\rangle\) state of the target qubit, cutting the number of states we are acting on in half each time). For \(n=4\), this amounts to \(2^4/4=4\) states, which is exactly what we need for the first term above. To apply the phase to the first four states, \(|0000\rangle\), \(|0001\rangle\), \(|0010\rangle\), and \(|0011\rangle\), we want to “filter by” the first two qubits being in the \(|0\rangle\) state. For qubit \(0\), we do this by controlling on the \(|0\rangle\) state. For qubit \(1\), we pick it as the target of the controlled phase shift operation. Generically, this would make it act on the \(|1\rangle\) state, so we simply flip qubit \(1\) before and after the operation to apply the phase to the \(|0\rangle\) state instead. Thus, we conclude this first step by applying the gates
qml.X(1)
,qml.ctrl(qml.PhaseShift(2 * phi, 1), control=[0], control_values=[0])
, andqml.X(1)
.Next, we implement the second term in the projector decomposition, applying a phase to a single computational basis state. This requires us to fully control a phase shift gate, i.e., we use the last qubit as target and the other three as controls (there is some freedom of choice here, but this is a convenient choice). We want to apply the phase to the state \(|3\rangle=|0011\rangle\). So the controls \(0\) and \(1\) are set to zero and the control \(2\) is set to one. As we want to effect the phase onto the \(|1\rangle\) state of qubit \(3\), we don’t need to flip the target bit as we did before. However, given the negative sign in the projector decomposition, we need to multiply the phase with \(-1\). Overall, we apply the gate
qml.ctrl(qml.PhaseShift(-2 * phi, 3), control=[0, 1, 2], control_values=[0, 0, 1])
, which concludes the decomposition, now reading:>>> print(qml.draw(op_3.decomposition)()) 0: ──GlobalPhase(1.23)────╭○───────────╭○─────────┤ 1: ──GlobalPhase(1.23)──X─╰Rϕ(2.46)──X─├○─────────┤ 2: ──GlobalPhase(1.23)─────────────────├●─────────┤ 3: ──GlobalPhase(1.23)─────────────────╰Rϕ(-2.46)─┤
- static compute_diagonalizing_gates(*params, wires, **hyperparams)¶
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.
See also
- Parameters:
params (list) – trainable parameters of the operator, as stored in the
parameters
attributewires (Iterable[Any], Wires) – wires that the operator acts on
hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparameters
attribute
- Returns:
list of diagonalizing gates
- Return type:
list[.Operator]
- static compute_eigvals(*params, **hyperparams)[source]¶
Get the eigvals for the Pi-controlled phase unitary.
- static compute_matrix(phi, dimension)[source]¶
Get the matrix representation of Pi-controlled phase unitary.
- static compute_qfunc_decomposition(*args, **hyperparameters)¶
Experimental method to compute the dynamic decomposition of the operator with program capture enabled.
When the program capture feature is enabled with
qml.capture.enable()
, the decomposition of the operator is computed with this method if it is defined. Otherwise, thecompute_decomposition()
method is used.The exception to this rule is when the operator is returned from the
compute_decomposition()
method of another operator, in which case the decomposition is performed withcompute_decomposition()
(even if this method is defined), and not with this method.When
compute_qfunc_decomposition
is defined for an operator, the control flow operations within the method (specifying the decomposition of the operator) are recorded in the JAX representation.Note
This method is experimental and subject to change.
See also
- Parameters:
*args (list) – positional arguments passed to the operator, including trainable parameters and wires
**hyperparameters (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparameters
attribute
- static compute_sparse_matrix(*params, format='csr', **hyperparams)¶
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
- Parameters:
*params (list) – trainable parameters of the operator, as stored in the
parameters
attributeformat (str) – format of the returned scipy sparse matrix, for example ‘csr’
**hyperparams (dict) – non-trainable hyperparameters of the operator, as stored in the
hyperparameters
attribute
- 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.See also
- Returns:
decomposition of the operator
- Return type:
list[Operator]
- 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.See also
- Returns:
a list of operators
- Return type:
list[.Operator] 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.See also
- Returns:
eigenvalues
- Return type:
tensor_like
- generator()[source]¶
Generator of the
PCPhase
operator, which is in single-parameter-form. The operator reads\[\Pi(\phi) = e^{i\phi (2\Pi - \mathbb{I}_N)},\]where \(\Pi\) is the projector onto the first :math`d` (
dim
) computational basis states and \(N=2^n\) is the Hilbert space dimension for \(n\) qubits.Correspondingly, the generator is \(2\Pi - \mathbb{I}_N=\text{diag}(\underset{d\text{ times}}{\underbrace{1, \dots, 1}},\underset{(N-d)\text{ times}}{\underbrace{-1, \dots, -1}})\):
>>> qml.PCPhase(0.5, dim=3, wires=[0, 1]).generator() Hermitian(array([[ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, -1]]), wires=[0, 1])
- label(decimals=None, base_label=None, cache=None)[source]¶
The label of the operator when displayed in a circuit.
- map_wires(wire_map)¶
Returns a copy of the current 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:
.Operator
- 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
- Parameters:
wire_order (Iterable) – global wire order, must contain all wire labels from the operator’s wires
- Returns:
matrix representation
- Return type:
tensor_like
- queue(context=<class 'pennylane.queuing.QueuingManager'>)¶
Append the operator to the Operator queue.
- single_qubit_rot_angles()¶
The parameters required to implement a single-qubit gate as an equivalent
Rot
gate, up to a global phase.- Returns:
A list of values \([\phi, \theta, \omega]\) such that \(RZ(\omega) RY(\theta) RZ(\phi)\) is equivalent to the original operation.
- Return type:
tuple[float, float, float]
- 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.See also
- 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[.Operation]]