Rotation and State Preparation¶
Rotation and state preparation operators provide arbitrary-dimensional unitary rotations and the ability to prepare a target quantum state from |0⟩.
Overview¶
Operator |
Operation |
Unitarity class |
|---|---|---|
|
Apply an arbitrary-dimensional unitary matrix |
BaseOperator |
|
Prepare a target quantum state from |
BaseOperator |
—
Rot_GeneralUnitary (general unitary rotation)¶
Operation: Applies an arbitrary \(2^n \times 2^n\) unitary matrix to a register.
Parameters:
reg— target register (name or ID)matrix— unitary matrix (DenseMatrix_complexor a NumPy array)
Dagger: Applies the conjugate transpose of the matrix.
Purpose: When the standard gate library cannot directly express the desired transformation, you can specify it directly in matrix form.
Note
The matrix dimension must match the Hilbert-space dimension \(2^n\) of the register, where \(n\) is the number of bits in the register.
import numpy as np
import pysparq as ps
ps.System.clear()
ps.System.add_register("q", ps.UnsignedInteger, 2)
state = ps.SparseState()
# Define a 4x4 unitary matrix
matrix = np.eye(4, dtype=complex)
matrix[0, 0] = 0
matrix[0, 3] = 1
matrix[3, 3] = 0
matrix[3, 0] = 1
op = ps.Rot_GeneralUnitary("q", matrix)
op(state)
# Undo
op.dag(state)
—
Rot_GeneralStatePrep (quantum state preparation)¶
Operation: Prepares a register from the |0⟩ state into a target quantum state.
Parameters:
reg— target register (name or ID)state_vector— the target state vector (list of complex numbers or a NumPy array)
Dagger: Maps the target state back to |0⟩.
Mathematical representation: Given a target state \(|\psi\rangle = \sum_i \alpha_i |i\rangle\), construct a unitary \(U\) such that \(U|0\rangle = |\psi\rangle\).
Warning
This operator requires the register to currently be in the |0⟩ state. If the register already holds a nonzero value, the behavior is undefined.
import numpy as np
import pysparq as ps
ps.System.clear()
ps.System.add_register("q", ps.UnsignedInteger, 2)
state = ps.SparseState()
# Prepare a simplified Bell state: a uniform superposition
target = np.array([0.5, 0.5, 0.5, 0.5], dtype=complex)
op = ps.Rot_GeneralStatePrep("q", target)
op(state)
ps.pprint(state)
# |q=0⟩ : (0.5+0j)
# |q=1⟩ : (0.5+0j)
# |q=2⟩ : (0.5+0j)
# |q=3⟩ : (0.5+0j)
Helper Functions¶
stateprep_unitary_build_schmidt¶
Operation: Constructs the unitary matrix needed for state preparation using a Schmidt decomposition.
Purpose: Use it when you need to manually build or inspect the internal matrix of Rot_GeneralStatePrep.