qml.estimator.templates.QROMStatePreparation¶
- class QROMStatePreparation(num_state_qubits, precision=None, positive_and_real=False, select_swap_depths=1, wires=None)[source]
Bases:
ResourceOperator
Resource class for the QROMStatePreparation template.
This operation implements the state preparation method described in arXiv:0208112, using
QROM
to dynamically load the rotation angles.Note
This decomposition assumes an appropriately sized phase gradient state is available. Users should ensure the cost of constructing such a state has been accounted for. See also
PhaseGradient
.- Parameters:
num_state_qubits (int) – number of qubits required to represent the statevector
precision (float) – the precision threshold for loading in the binary representation of the rotation angles
positive_and_real (bool) – indicates whether or not the coefficients of the statevector are all real and positive
select_swap_depths (int | Iterable(int) | None) – A parameter of
QROM
used to trade-off extra qubits for reduced circuit depth. Can beNone
,1
or a positive integer power of two. Defaults toNone
, which internally corresponds to the optimal depth.wires (WiresLike | None) – The wires on which to prepare the target state. This excludes any additional qubits allocated during the decomposition (via select-swap).
- Resources:
The resources for QROMStatePreparation are computed according to the decomposition described in arXiv:0208112, using
QROM
to dynamically load the rotation angles. These rotations gates are implemented using an in-place controlled-adder operation (see figure 4. of arXiv:2409.07332) to a phase gradient state.
See also
Example
The resources for this operation are computed using:
>>> qrom_prep = qre.QROMStatePreparation(num_state_qubits=5, precision=1e-3) >>> print(qre.estimate(qrom_prep)) --- Resources: --- Total wires: 28 algorithmic wires: 5 allocated wires: 23 zero state: 23 any state: 0 Total gates : 2.756E+3 'Toffoli': 236, 'CNOT': 1.522E+3, 'X': 230, 'Z': 12, 'S': 24, 'Hadamard': 732
Usage Details
This operation uses the
QROM
subroutine to dynamically load the rotation angles.>>> import pennylane.estimator as qre >>> gate_set = {"QROM", "Hadamard", "CNOT", "T", "Adjoint(QROM)"} >>> qrom_prep = qre.QROMStatePreparation( ... num_state_qubits = 4, ... precision = 1e-2, ... select_swap_depths = 1, ... ) >>> res = qre.estimate(qrom_prep, gate_set) >>> print(res) --- Resources: --- Total wires: 21 algorithmic wires: 4 allocated wires: 17 zero state: 17 any state: 0 Total gates : 2.680E+3 'QROM': 5, 'Adjoint(QROM)': 5, 'T': 1.832E+3, 'CNOT': 580, 'Hadamard': 258
The
precision
argument is used to allocate the target wires in the underlying QROM operations. It corresponds to the precision with which the rotation angles of the template are encoded. This means that the binary representation of the angle is truncated up to the \(m\)-th digit, where \(m\) is the number of precision wires allocated. See Eq. 5 in arXiv:0208112 for more details.The
select_swap_depths
parameter allows a user to configure theselect_swap_depth
of each individualQROM
used. Theselect_swap_depths
argument can be one of(int, None, Iterable(int, None))
.If an integer or
None
is passed (the default value for this parameter is 1), then that is used as theselect_swap_depth
for allQROM
operations in the resource decomposition.>>> print(res.gate_breakdown()) Adjoint(QROM) total: 5 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=9, params={'num_bit_flips':4, 'num_bitstrings':1, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=10, params={'num_bit_flips':9, 'num_bitstrings':2, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=11, params={'num_bit_flips':18, 'num_bitstrings':4, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=12, params={'num_bit_flips':36, 'num_bitstrings':8, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=13, params={'num_bit_flips':72, 'num_bitstrings':16, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 QROM total: 5 QROM {'num_bit_flips': 4, 'num_bitstrings': 1, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 9, 'num_bitstrings': 2, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 18, 'num_bitstrings': 4, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 36, 'num_bitstrings': 8, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 72, 'num_bitstrings': 16, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 T total: 1.832E+3 CNOT total: 580 Hadamard total: 258
Alternatively, we can configure each value independently by specifying a list. Note the size of this list should be
num_state_qubits + 1
(ornum_state_qubits
if the state is positive and real).>>> qrom_prep = qre.QROMStatePreparation( ... num_state_qubits = 4, ... precision = 1e-2, ... select_swap_depths = [1, None, 1, 1, None], ... ) >>> res = qre.estimate(qrom_prep, gate_set) >>> print(res.gate_breakdown()) Adjoint(QROM) total: 5 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=9, params={'num_bit_flips':4, 'num_bitstrings':1, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=10, params={'num_bit_flips':9, 'num_bitstrings':2, 'restored':False, 'select_swap_depth':None, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=11, params={'num_bit_flips':18, 'num_bitstrings':4, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=12, params={'num_bit_flips':36, 'num_bitstrings':8, 'restored':False, 'select_swap_depth':1, 'size_bitstring':9})}: 1 Adjoint(QROM) {'base_cmpr_op': CompressedResourceOp(QROM, num_wires=13, params={'num_bit_flips':72, 'num_bitstrings':16, 'restored':False, 'select_swap_depth':None, 'size_bitstring':9})}: 1 QROM total: 5 QROM {'num_bit_flips': 4, 'num_bitstrings': 1, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 9, 'num_bitstrings': 2, 'restored': False, 'select_swap_depth': None, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 18, 'num_bitstrings': 4, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 36, 'num_bitstrings': 8, 'restored': False, 'select_swap_depth': 1, 'size_bitstring': 9}: 1 QROM {'num_bit_flips': 72, 'num_bitstrings': 16, 'restored': False, 'select_swap_depth': None, 'size_bitstring': 9}: 1 T total: 1.832E+3 CNOT total: 580 Hadamard total: 258
Attributes
Returns a dictionary containing the minimal information needed to compute the resources.
- resource_keys = {'num_state_qubits', 'positive_and_real', 'precision', 'selswap_depths'}¶
- resource_params¶
Returns a dictionary containing the minimal information needed to compute the resources.
- Returns:
- A dictionary containing the resource parameters:
num_state_qubits (int): number of qubits required to represent the state-vector
precision (float): the precision threshold for loading in the binary representation of the rotation angles
positive_and_real (bool): flag that the coefficients of the statevector are all real and positive
selswap_depths (int | Iterable(int) | None): a parameter of
QROM
used to trade-off extra qubits for reduced circuit depth
- Return type:
dict
Methods
controlled_ry_resource_decomp
(...[, ...])Returns a list representing the resources of the operator.
resource_decomp
(num_state_qubits, ...[, ...])Returns a list representing the resources of the operator.
resource_rep
(num_state_qubits[, precision, ...])Returns a compressed representation containing only the parameters of the Operator that are needed to compute the resources.
- classmethod controlled_ry_resource_decomp(num_state_qubits, positive_and_real, precision=None, selswap_depths=1)[source]¶
Returns a list representing the resources of the operator. Each object in the list represents a gate and the number of times it occurs in the circuit.
- Parameters:
num_state_qubits (int) – number of qubits required to represent the state-vector
positive_and_real (bool) – Flag that the coefficients of the statevector are all real and positive.
precision (float) – The precision threshold for loading in the binary representation of the rotation angles.
selswap_depths (int | Iterable(int) | None) – A parameter of
QROM
used to trade-off extra qubits for reduced circuit depth.
- Resources:
The resources for QROMStatePreparation are according to the decomposition as described in arXiv:0208112, using
QROM
to dynamically load the rotation angles. Controlled-RY (and phase shifts) gates are used to apply all of the rotations coherently.
- Returns:
A list of
GateCount
objects, where each object represents a specific quantum gate and the number of times it appears in the decomposition.- Return type:
list[
GateCount
]
- classmethod resource_decomp(num_state_qubits, positive_and_real, precision=None, selswap_depths=1)[source]¶
Returns a list representing the resources of the operator. Each object in the list represents a gate and the number of times it occurs in the circuit.
Note
This decomposition assumes an appropriately sized phase gradient state is available. Users should ensure the cost of constructing such a state has been accounted for. See also
PhaseGradient
.- Parameters:
num_state_qubits (int) – number of qubits required to represent the state-vector
positive_and_real (bool) – Flag that the coefficients of the statevector are all real and positive.
precision (float) – The precision threshold for loading in the binary representation of the rotation angles.
selswap_depths (int | Iterable(int) | None) – A parameter of
QROM
used to trade-off extra qubits for reduced circuit depth.
- Resources:
The resources for QROMStatePreparation are according to the decomposition as described in arXiv:0208112, using
QROM
to dynamically load the rotation angles. These rotations gates are implmented using an inplace controlled-adder operation (see figure 4. of arXiv:2409.07332) to phase gradient.
- Returns:
A list of
GateCount
objects, where each object represents a specific quantum gate and the number of times it appears in the decomposition.- Return type:
list[
GateCount
]
- classmethod resource_rep(num_state_qubits, precision=None, positive_and_real=False, selswap_depths=1)[source]¶
Returns a compressed representation containing only the parameters of the Operator that are needed to compute the resources.
- Parameters:
num_state_qubits (int) – number of qubits required to represent the state-vector
precision (float) – the precision threshold for loading in the binary representation of the rotation angles
positive_and_real (bool) – flag that the coefficients of the statevector are all real and positive
selswap_depths (int | Iterable(int) | None) – a parameter of
QROM
used to trade-off extra qubits for reduced circuit depth
- Returns:
the operator in a compressed representation
- Return type: