qml.estimator.templates.IntegerComparator

class IntegerComparator(value, register_size=None, geq=False, wires=None)[source]

Bases: ResourceOperator

This operation applies a controlled X gate using integer comparison as the condition.

Given a basis state \(\vert n \rangle\), where \(n\) is a positive integer, and a fixed positive integer \(L\), a target qubit is flipped if \(n \geq L\). Alternatively, the flipping condition can be \(n \lt L\).

Parameters:
  • value (int) – The value \(L\) that the state’s decimal representation is compared against.

  • register_size (int | None) – size of the register for basis state

  • geq (bool) – If set to True, the comparison made will be \(n \geq L\). If False, the comparison made will be \(n \lt L\).

  • wires (WiresLike | None) – the wires the operation acts on

Resources:

This decomposition uses the minimum number of MultiControlledX gates required for the given integer value. The given integer is first converted into its binary representation, and compared to the quantum register iteratively, starting with the most significant bit, and progressively including more qubits. For example, when geq is False, value is \(22\) (Binary \(010110\)), and num_wires is \(6\):

  • Evaluating most significant bit: For all \(6\)-bit numbers where the first two control qubits are in the \(00\) state, \(n \lt 22\) condition is always True. A MultiControlledX gate can be applied with these two wires as controls and control values corresponding to \(00\).

  • Refining with subsequent bits: Considering the next most significant bit, since the target value begins with \(0101\). Therefore, all \(6\)-bit numbers beginning with \(0100\) will satisfy the condition, so a MultiControlledX gate can be applied with the first four wires as controls and control values corresponding to \(0100\).

  • This iterative procedure continues, with MultiControlledX gates being added for each significant bit of the target value, until the full conditional operation is realized with the minimum number of multi-controlled operations.

The circuit which applies the comparison operation for the above example is defined as:

0: ────╭○─╭○─╭○─┤
1: ────├○─├●─├●─┤
2: ────│──├○─├○─┤
3: ────│──├○─├●─┤
4: ────│──│──├○─┤
5: ──-─│──│──│──┤
6: ────╰X─╰X─╰X─┤

Example

The resources for this operation are computed using:

>>> import pennylane.estimator as qre
>>> integer_compare = qre.IntegerComparator(value=4, register_size=6)
>>> print(qre.estimate(integer_compare))
--- Resources: ---
 Total wires: 9
    algorithmic wires: 7
    allocated wires: 2
         zero state: 2
         any state: 0
 Total gates : 19
  'Toffoli': 3,
  'CNOT': 2,
  'X': 8,
  'Hadamard': 6

resource_keys

resource_params

Returns a dictionary containing the minimal information needed to compute the resources.

resource_keys = {'geq', 'register_size', 'value'}
resource_params

Returns a dictionary containing the minimal information needed to compute the resources.

Returns:

A dictionary containing the resource parameters:
  • value (int): The value \(L\) that the state’s decimal representation is compared against.

  • register_size (int): size of the register for basis state

  • geq (bool): If set to True, the comparison made will be \(n \geq L\). If False, the comparison made will be \(n \lt L\).

Return type:

dict

resource_decomp(value, register_size[, geq])

Returns a list representing the resources of the operator.

resource_rep(value, register_size[, geq])

Returns a compressed representation containing only the parameters of the Operator that are needed to compute the resources.

classmethod resource_decomp(value, register_size, geq=False)[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:
  • value (int) – The value \(L\) that the state’s decimal representation is compared against.

  • register_size (int) – size of the register for basis state

  • geq (bool) – If set to True, the comparison made will be \(n \geq L\). If False, the comparison made will be \(n \lt L\).

Resources:

This decomposition uses the minimum number of MultiControlledX gates. The given integer is first converted into its binary representation, and compared to the quantum register iteratively, starting with the most significant bit, and progressively including more qubits. For example, when geq is False, value is \(22\) (Binary \(010110\)), and num_wires is \(6\):

  • Evaluating most significant bit: For all \(6\)-bit number where the first two control qubits are in the \(00\) state, \(n \lt 22\) condition is always True. A MultiControlledX gate can be applied with these two wires as controls and control values corresponding to \(00\).

  • Refining with subsequent bits: Considering the next most significant bit, since the target value begins with \(0101\). Therefore, all \(6\)-bit numbers beginning with \(0100\) will satisfy the condition, so a MultiControlledX gate can be applied with the first four wires as controls and control values corresponding to \(0100\).

  • This iterative procedure continues, with MultiControlledX gates being added for each significant bit of the target value, until the full conditional operation is realized with the minimum number of multi-controlled operations.

The circuit which applies the comparison operation for the above example is defined as:

0: ────╭○─╭○─╭○─┤
1: ────├○─├●─├●─┤
2: ────│──├○─├○─┤
3: ────│──├○─├●─┤
4: ────│──│──├○─┤
5: ──-─│──│──│──┤
6: ────╰X─╰X─╰X─┤
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(value, register_size, geq=False)[source]

Returns a compressed representation containing only the parameters of the Operator that are needed to compute the resources.

Parameters:
  • value (int) – The value \(L\) that the state’s decimal representation is compared against.

  • register_size (int) – size of the register for basis state

  • geq (bool) – If set to True, the comparison made will be \(n \geq L\). If False, the comparison made will be \(n \lt L\).

Returns:

the operator in a compressed representation

Return type:

CompressedResourceOp

Contents

Using PennyLane

Release news

Development

API

Internals