Iterable Demuxing#
- demux(input, ctrl_qv, output=None, ctrl_method=None, permit_mismatching_size=False, parallelize_qc=False)[source]#
This functions allows moving an input value into an iterable output, where the position is specified by a
QuantumFloat
. Demux is short for demultiplexer and is a standard component in classical electrical circuitry.Demux can either move qubit states into a QuantumVariable or
QuantumVariables
intoQuantumArrays
.This function can also be used to “in-place demux” the 0-th entry of an iterable to the position specified by
ctrl_qv
. For more information on this, check the second example.- Parameters:
- inputQubit or QuantumVariable
The input value that is supposed to be moved.
- ctrl_qvQuantumFloat
The QuantumFloat specifying to which output the input should be moved.
- outputQuantumVariable or QuantumArray, optional
The output object, where the input should end up. By default, a new object (QuantumVariable or QuantumArray) is created. Note that when this parameter is given, it is guaranteed, that the 0-th entry will be moved to the desired position, the other entries can also be permuted away from their original position.
- ctrl_methodstring, optional
The
ctrl_method
string passed to the control environment to generate controlled swaps.- permit_mismatching_sizebool, optional
If set to False, an exception will be raised, if the state-space dimension of ctrl_qv` is differing from the amount of outputs. The default is False.
- parallelize_qcbool, optional
If set to True, this option reduces (de)allocates additional qubits to reduce the depth. The default is False.
- Returns:
- outputQuantumVariable or QuantumArray
The output object with the input signal placed at the index specified by
ctrl_qv
.
- Raises:
- Exception
Tried to demux with mismatchingly sized control input.
Examples
We create a
QuantumBool
and demux it into aQuantumArray
from qrisp import * qb = QuantumBool() qb.flip() index = QuantumFloat(2) h(index[1]) res_array = demux(qb, index)
>>> print(multi_measurement([index, res_array])) {(0, OutcomeArray([1., 0., 0., 0.])): 0.5, (2, OutcomeArray([0., 0., 1., 0.])): 0.5}
Demux can also be used to move the 0-th entry of a
QuantumArray
in-place.qa = QuantumArray(shape = 4, qtype = qb) qa[0].flip() demux(qa[0], index, qa)
>>> print(multi_measurement([index, qa])) {(0, OutcomeArray([1., 0., 0., 0.])): 0.5, (2, OutcomeArray([0., 0., 1., 0.])): 0.5}
For low-level manipulations, demux can move information within
QuantumVariables
.qf = QuantumVariable(4) qf[:] = "1000" demux(qf[0], index, qf)
>>> print(multi_measurement([index, qf])) {(0, '1000'): 0.5, (2, '0010'): 0.5}