View source on GitHub |
Left-multiplies an NxN matrix onto N slices of a numpy array.
cirq.apply_matrix_to_slices(
target: np.ndarray,
matrix: np.ndarray,
slices: Sequence[_TSlice],
*,
out: Optional[np.ndarray] = None
) -> np.ndarray
One example is that the 4x4 matrix of a fractional SWAP gate can be expressed as
\(\) \begin{bmatrix} 1 & & \ & X**t & \ & & 1 \ \end{bmatrix}
Where X is the 2x2 Pauli X gate and t is the power of the swap with t=1
being a full swap. X**t is a power of the Pauli X gate's matrix.
Applying the fractional swap is equivalent to applying a fractional X
within the inner 2x2 subspace; the rest of the matrix is identity. This
can be expressed using apply_matrix_to_slices
as follows:
def fractional_swap(target):
assert target.shape == (4,)
return apply_matrix_to_slices(
target=target,
matrix=cirq.unitary(cirq.X**t),
slices=[1, 2]
)
Returns | |
---|---|
The transformed array. |
Raises | |
---|---|
ValueError
|
If out is target , or the matrix shaped does not match
slices .
|