quadratic_form#
Functionality#
This program implements a quadratic form on binary variables encoded in qubit registers.
A quadratic form on binary variables is a quadratic function \(Q(x)\) acting on a binary variable of \(n\) bits, \(x=x_{n-1}\cdots x_n\). For an integer matrix \(A\), an integer vector \(b\) and an integer \(c\), the function can be written as
If \(A\in\mathbb{Z}^{n \times n}, b\in\mathbb{Z}^{n \times 1}\) or \(c\in\mathbb{Z}\) contain scalar values, this circuit computes only an approximation of the quadratic form.
quadratic_form.py#
Callee: QuadraticForm
Provided with \(m\) qubits to encode the value, this circuit computes \(Q(x) \mod 2^m\) in two’s complement representation:
We use two’s complement bit pattern to interpret the \(m\)-bit output as a signed integer. If the most significant bit is 0, the positive value is stored as \(Q(x)\mod 2^m\). Otherwise, the negative value is stored as \((Q(x) + 2^m) \mod 2^m\). Equivalently, the rule is:
For example, the value of \(Q(x)=3\) is ‘011’, where the first 0 indicates a positive value and 11 is the value 3. \(Q(x)=-3\) would be transformed as \(-3+2^3=5\), i.e., ‘101’.
If the value of \(Q(x)\) is too large to be represented with m qubits, the resulting bitstring is \((Q(x) + 2^m) \mod 2^m\).
The implementation of this circuit is discussed in [32], Fig. 6.
Test expectations:
The probability of the basis state which corresponds with bitstring \((Q(x) + 2^m) \mod 2^m\) will be 1 upon measurement. When \(Q(x)\) is negative, the corresponding state is \(2^m+Q(x)\).
API#
qolumbina.programs.quadratic_form.quadratic_form#
A circuit implementing a quadratic form on binary variables.
- class QuadraticForm(num_result_qubits=None, quadratic=None, linear=None, offset=None, little_endian=True)[source]#
Bases:
QuantumCircuitImplements a quadratic form on binary variables encoded in qubit registers.
- Parameters:
num_result_qubits (Optional[int]) – The number of qubits used to encode the result of the quadratic form, i.e., \(m\).
quadratic (Optional[Union[np.ndarray, List[List[Union[float, ParameterExpression]]]]]) – The quadratic coefficients matrix \(A\). The default is None. If None, it is assumed to be a zero matrix.
linear (Optional[Union[np.ndarray, List[Union[float, ParameterExpression]]]]) – The linear coefficients vector \(b\). The default is None. If None, it is assumed to be a zero vector.
offset (Optional[Union[float, ParameterExpression]]) – the offset scalar \(c\). The default is None. If None, it is assumed to be zero.
little_endian (bool) – Whether the input qubits are in little-endian order. Default is True.
- Raises:
ValueError – If
linearandquadratichave mismatching sizes.ValueError – If
num_result_qubitsis unspecified but cannot be determined because some values of the quadratic form are parameterized.
- static required_result_qubits(quadratic, linear, offset)[source]#
Get the number of required result qubits.
- Parameters:
quadratic (ndarray | List[List[float]]) – A matrix containing the quadratic coefficients.
linear (ndarray | List[float]) – An array containing the linear coefficients.
offset (float) – A constant offset.
- Returns:
The number of qubits needed to represent the value of the quadratic form in twos complement.
- Return type:
int
- class QuadraticFormGate(num_result_qubits=None, quadratic=None, linear=None, offset=None, label='Q(x)')[source]#
Bases:
GateImplements a quadratic form on binary variables encoded in qubit registers.
- Parameters:
num_result_qubits (int | None)
quadratic (Sequence[Sequence[float]] | None)
linear (Sequence[Sequence[float]] | None)
offset (float | None)
label (str)
- static required_result_qubits(quadratic, linear, offset)[source]#
Get the number of required result qubits.
- Parameters:
quadratic (Sequence[Sequence[float]]) – A matrix containing the quadratic coefficients.
linear (Sequence[float]) – An array containing the linear coefficients.
offset (float) – A constant offset.
- Returns:
The number of qubits needed to represent the value of the quadratic form in twos complement.
- Return type:
int