Utilities

Lightworks also has a number of utilities which aim to make using the package easier, offering random matrix generation and quantity conversion. Each utility function is detailed in a dedicated subsection.

lightworks.random_unitary()

Generates an NxN random unitary matrix using the SciPy unitary_group function. There is also an optional random seed which enables the same random matrix to be generated repeatedly.

# Create random 8x8 matrix
U = lw.random_unitary(8)

# Create 8x8 matrix with random seed to return the same matrix
U = lw.random_unitary(8, seed = 4)

Note

Random seed values provided within Lightworks typically need to be integers.

lightworks.random_permutation()

Generates an NxN permutation matrix, which is a unitary matrix with only values of 0 & 1. This is equivalent to swapping modes in a circuit. There is also an optional random seed which enables the same random matrix to be generated repeatedly.

# Create random 8x8 matrix
U = lw.random_permutation(8)

# Create 8x8 matrix with random seed to return the same matrix
U = lw.random_permutation(8, seed = 4)

lightworks.convert.db_loss_to_decimal()

Converts a positive dB loss into a decimal loss value, which can be used with the circuit loss elements.

# Convert 3dB loss into decimal
loss = lw.db_loss_to_decimal(3)
print(loss)
# Output: 0.49881276637272776

lightworks.convert.decimal_to_db_loss()

Converts a decimal loss value into a positive dB loss.

# Convert 30% loss into dB value
loss = lw.decimal_to_db_loss(0.3)
print(loss)
# Output: 1.5490195998574319

lightworks.convert.qubit_to_dual_rail()

Converts from qubit to dual-rail encoding.

dr_state = lw.qubit_to_dual_rail([1, 0, 1])
print(dr_state)
# Output: |0,1,1,0,0,1>

lightworks.convert.dual_rail_to_qubit()

Converts from a dual-rail encoding on pairs of adjacent modes into a qubit state.

qubit_state = lw.dual_rail_to_qubit([1, 0, 0, 1, 1, 0])
print(qubit_state)
# Output: |0,1,0>

lightworks.convert.threshold_mapping()

Applies a threshold mapping to the provided state.

qubit_state = lw.threshold_mapping([1, 2, 0, 3, 1, 0])
print(qubit_state)
# Output: |1,1,0,1,1,0>

lightworks.convert.parity_mapping()

Applies a parity mapping to the provided state, in which even & odd values are mapped to 0 & 1 respectively.

qubit_state = lw.parity_mapping([1, 2, 0, 3, 1, 0])
print(qubit_state)
# Output: |1,0,0,1,1,0>