Sorting¶
In this tutorial, we will go through Workbench Algorithms' implementation of one of the most ubiquitous computational primitives - sorting.
Sorting a sequence of numbers \(i_1, i_2, \ldots, i_n\) means reordering it into either an ascending sequence \(j_1 \le j_2 \le \ldots \le j_n\), or a descending sequence \(j_1 \ge j_2 \ge \ldots \ge j_n\).
For a quantum analogue of this procedure, we need an operation that performs such a reordering on each basis state, i.e.:
with \(j_1, j_2, \ldots, j_n\) ordered appropriately. Currently, this can be achieved in Workbench Algorithms using BitonicSort Qubrick. We won't go into details of how the bitonic sort algorithm works, and we'll focus on practical considerations for using this Qubrick instead. For more information about the bitonic sort algorithm itself, see this Wikipedia page ⧉.
from psiqdk.algorithms import BitonicSort
from psiqdk.algorithms.utils import total_sort_num_comps
from psiqdk.workbench import QPU, Qubits, Qubrick
Bitonic sort¶
The following code snippet demonstrates how to use the BitonicSort Qubrick.
# Helper for computing number of qubits needed for sorting (including input qubits)
def total_qubits_for_sorting(bit_length: int, num_inputs: int) -> int:
num_target = bit_length * num_inputs # Total size of target registers
num_aux = total_sort_num_comps(num_inputs) + bit_length # Total number of auxiliary qubits
return num_target + num_aux
bit_length = 2 # Number of bits sufficient for representing each input value
num_inputs = 3 # Number of inputs to sort
num_qubits = total_qubits_for_sorting(bit_length, num_inputs)
qpu = QPU(num_qubits=num_qubits)
inputs = [3, 0, 1]
target_regs = [Qubits(bit_length, f"val_{i}", qpu) for i in range(num_inputs)]
for reg, val in zip(target_regs, inputs):
reg.write(val)
sort_qbk = BitonicSort()
sort_qbk.compute(target_regs, direction=0) # direction=0 -> sorting in ascending order
print(f"Inputs: {inputs}")
print(f"Expected result: {sorted(inputs)}")
print(f"Result: {[reg.read() for reg in target_regs]}")
qpu.draw(show_qubricks=True)
Inputs: [3, 0, 1] Expected result: [0, 1, 3] Result: [0, 1, 3]
Indeed, you can see that the input state was sorted correctly. Let's see now how this procedure works for a state in superposition. To make this example a bit more interesting, we will sort in descending order, which can be achieved by passing direction=1 to BitonicSort.compute.
qpu = QPU(num_qubits=num_qubits)
inputs = [3, 0] # Two deterministic inputs, last Qubit will be prepared in superposition.
target_regs = [Qubits(bit_length, f"val_{i}", qpu) for i in range(num_inputs)]
for reg, val in zip(target_regs, inputs):
reg.write(val)
target_regs[-1].had()
print("State vector before sorting:")
qpu.print_state_vector()
sort_qbk = BitonicSort()
sort_qbk.compute(target_regs, direction=1)
print("State vector after sorting:")
qpu.print_state_vector();
State vector before sorting: |val_0|val_1|val_2|?> |3|0|0|.> 0.500000+0.000000j |3|0|1|.> 0.500000+0.000000j |3|0|2|.> 0.500000+0.000000j |3|0|3|.> 0.500000+0.000000j State vector after sorting: |val_0|val_1|val_2|record|record|record|?> |3|0|0|0|0|0|.> 0.500000+0.000000j |3|1|0|0|1|0|.> 0.500000+0.000000j |3|2|0|0|1|0|.> 0.500000+0.000000j |3|3|0|0|1|0|.> 0.500000+0.000000j
As expected, we obtained a superposition of sorted states.
Note that this Qubrick does not immediately uncompute the auxiliary qubits record; they remain entangled with the main registers until we call the uncompute method.
When constructing inputs for BitonicSort, you must ensure that all input registers have the same size. Otherwise, the program will fail with an exception, as demonstrated in the following example:
qpu = QPU(num_qubits=18)
inputs = [5, 3, 6]
# Target regs will have sizes 3, 3, and 4
target_regs = [Qubits(bit_length, f"val_{i}", qpu) for i, bit_length in enumerate([3, 3, 4], start=1)]
for reg, val in zip(target_regs, inputs):
reg.write(val)
sort_qbk = BitonicSort()
try:
sort_qbk.compute(target_regs, direction=0)
except ValueError as e:
print("Sorting failed. The error was:")
print(e)
Sorting failed. The error was: Error swapping qubit regs val_2 and val_3: registers are different sizes (3 and 4)
Before concluding this tutorial, let us mention that the BitonicSort Qubrick uses measurement-based uncomputation rather than replaying the daggers of gates in reverse order.
Summary¶
In this tutorial, we demonstrated how to sort sequence of registers using Workbench Algorithms.
BitonicSortQubrick can sort a sequence of same-sized registers.- The order of sorting is controlled by
directionargument. Passdirection=0for ascending order, anddirection=1for descending order.