exact_reciprocal#
Functionality#
Implements an exact reciprocal function within a quantum circuit.
exact_reciprocal.py#
Callee: ExactReciprocal
For a \(n\)-qubit computational state \(\ket{x}_n\) and a scaling factor \(s\), this circuit implements the operation to compute the reciprocal 1/x:
States representing \(x = 0\) or \(s\frac{2^n}{x} \geq 1\) are left unchanged, i.e., \(\ket{0}_1\ket{x}_n\), since this function would not be defined.
Test expectations
The reciprocal result is stored proportionally to the \(\ket{1}\) amplitude of the ancilla qubit, i.e., \(\frac{s}{2^nx}\). \(x\) is either an unsigned integer (neg_vals=False) or converted into the according signed fractional bits (neg_vals=True).
For invalid \(x\), the state remains unchanged and the measurement on the ancilla qubit always produces 0. For valid \(x\), the probability of measuring the ancilla qubit as 1 should match the probability as \(\left(\frac{s}{2^nx}\right)^2\) within sampling error.
API#
qolumbina.programs.exact_reciprocal.exact_reciprocal#
Exact reciprocal rotation.
- class ExactReciprocal(num_state_qubits, scaling, neg_vals=False, name='1/x')[source]#
Bases:
QuantumCircuitExact reciprocal
- Parameters:
num_state_qubits (int) – The number of qubits representing the value to invert, i.e., \(n\).
scaling (float) – Scaling factor \(s\) of the reciprocal function, i.e. to compute \(s / x\).
neg_vals (bool) –
Whether to allow negative values for the input state, where the default is
False.- If
False: The state register represents an unsigned integer \(x \in \{0, ..., 2^n-1\}\), and the circuit performs an operation to make the \(\ket{1}\) amplitude of the ancilla qubit proportional to target result \(1/x\).
- If
- If
True: The most significant qubit (i.e.,
qr_state[-1]) of \(\ket{x}_n\) is the sign, with \(\ket{1}\) for negative and \(\ket{0}\) for positive. The remaining $n-1$ qubits encode a fractional value \(j/2^{n-1} \in [0, 1)\). This is motivated by the fact that the state register originates from phase estimation where a negative phase \(-x\) is indistinguishable from \(1-x\) since \(e^{-2\pi ix} = e^{2\pi i(1 - x)}\) for \(x \in [0, 1)\). Therefore, the register holds the wrapped value \(1-x\) in its fractional bits instead of the original \(-x\). In other words, if the sign bit is 1, the fractional bits are interpreted as \(1-x\) while the reciprocal result is computed using the absolute value of \(|-x|\), i.e., \(x\).
- If
name (str) – The name of the object.
Note
It is assumed that the binary string \(x\) represents a number < 1.
- class ExactReciprocalGate(num_state_qubits, scaling, neg_vals=False, label='1/x')[source]#
Bases:
GateImplements an exact reciprocal function.
For a state \(|x\rangle\) and a scaling factor \(s\), this gate implements the operation
\[|x\rangle |0\rangle \mapsto \cos\left(\arcsin\left(s\frac{2^n}{x}\right)\right)|x\rangle|0\rangle + \left(s\frac{2^n}{x}\right)|x\rangle|1\rangle.\]States representing \(x = 0\) or \(s 2^n / x \geq 1\) are left unchanged, since this function would not be defined.
- Parameters:
num_state_qubits (int) – The number of qubits representing the value to invert.
scaling (float) – Scaling factor \(s\) of the reciprocal function, i.e. to compute \(s / x\).
neg_vals (bool) – Whether \(x\) might represent negative values.
label (str) – The label of the object.
Note
It is assumed that the binary string \(x\) represents a number < 1.