Reference: Qubricks¶
Contents¶
Qubrick Class¶
psiqworkbench.Qubrick ¶
The base class for defining quantum computational units (Qubricks).
A Qubrick is a modular quantum algorithm component that provides:
- Automatic uncomputation of quantum operations.
- Management of temporary qubit registers and auxiliary qubits.
- Customizable control flow with symbolic computations.
- Resource estimation via Workbench.
For more details, see High-Level Routines: Qubricks tutorial.
Settings
do_gates: IfTrue, implements actual quantum gates.alloc_result: IfTrue, places the result in a newly allocated register.release_ancillae: IfTrue, releases unused auxiliary qubits after uncompute.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the Qubrick. |
do_gates |
bool
|
Whether quantum gates should be executed. |
show_allocated |
bool
|
If |
alloc_result |
bool
|
Whether the result is stored in an allocated register. |
release_ancillae |
bool
|
Whether unused auxiliary qubits are released. |
allow_multi_qubit_ctrl |
bool
|
If |
debug_check |
bool
|
Enables debug checks for errors like overflows. |
debug_check_warnings |
bool
|
If |
rotation_error_param |
float
|
The error parameter used for synthesized rotations. |
dagger |
bool
|
Whether the Qubrick applies its dagger (inverse operation). |
qc |
QPU or None
|
The associated QPU instance. |
modifies_state_vector_directly |
bool
|
Whether the Qubrick modifies the state vector non-physically. |
never_uncompute |
bool
|
If |
use_custom_uncompute |
bool
|
If |
skip_recompute_playback |
bool
|
If |
_cs_stack |
deque
|
Stack of |
Example
class Parity(Qubrick):
def _compute(self, input_qreg):
anc = self.alloc_temp_qreg(1, 'anc')
self.set_result_qreg(anc)
anc.lelbow(input_qreg[0])
for bit in range(1, input_qreg.num_qubits):
anc.x(input_qreg[bit])
qc = QPU(num_qubits=5)
reg = Qubits(4, "reg", qc)
parity = Parity()
with parity.computed(reg) as result:
result.z()
Notes
- The
compute()anduncompute()methods handle execution logic. - Use
computed()as a context manager for automatic uncompute. - Implements daggered operations if
dagger=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A name for this Qubrick |
None
|
**kwargs
|
Any
|
Settings for this qubrick. A full list can be seen in the QubrickSettings dataclass. |
{}
|
get_cs ¶
Return the current QubrickComputeState off the stack.
Returns:
| Type | Description |
|---|---|
QubrickComputeState
|
The current compute state. |
alloc_temp_qreg ¶
Allocates a temporary qubit register for use in compute().
This function:
- Allocates a new Qubits register if not in symbolic mode.
- Allocates a SymbolicQubits register if
is_symbolic=True. - Handles automatic qubit release based on
release_after_computeandrelease_after_uncompute.
For more details, see Auxiliary Qubit Management in Qubricks tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_qubits
|
int or Parameter
|
Number of qubits to allocate. |
required |
name
|
str
|
The name of the allocated register. |
required |
release_after_compute
|
bool
|
If |
False
|
release_after_uncompute
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Qubits or SymbolicQubits
|
The allocated qubit register. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
set_result_qreg ¶
Set a named Qubits-based result, such as a comparison result or the output of a quantum addition.
This may be a newly-allocated register, or an existing register.
Note that this is stored in the QubrickComputeState, so it's safe to use for deep Qubrick call stacks.
For more details, see Auxiliary Qubit Management in Qubricks tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qreg
|
Qubits
|
The Qubits register to set as the result. |
required |
name
|
str
|
The name of the result param to set. Default is |
'result'
|
get_result_qreg ¶
Get a named Qubits-based result, such as a comparison result or the output of a quantum addition.
This may be a newly-allocated register, or an existing register.
Note that this is stored in the QubrickComputeState, so it's safe to use for deep Qubrick call stacks.
For more details, see Auxiliary Qubit Management in Qubricks tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the result param to get. Default is |
'result'
|
set_classical_result ¶
Set a named classical result, such as measurement results, to be returned from the compute() call.
Note that this is stored in the QubrickComputeState, so it's safe to use for deep Qubrick call stacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_data
|
Any
|
The value to set as the result. |
required |
name
|
str
|
The name of the result param to set. Default is |
'result'
|
get_classical_result ¶
Get a named classical result, such as measurement results from the compute() call.
Note that this is stored in the QubrickComputeState, so it's safe to use for deep Qubrick call stacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the result param to get. Default is |
'result'
|
get_input_qreg ¶
Get a named Qubits-based input which was passed at compute() call, such as the inputs to a quantum addition.
Note that this is stored in the QubrickComputeState, so it's safe to use for deep Qubrick call stacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the input to get. |
required |
set_debug_error ¶
Indicate that an error has occurred.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regs
|
list of Qubits
|
The registers involved in the error. |
required |
message
|
str
|
A description of the problem. |
required |
estimate ¶
Alias for compute(), used when running in symbolic mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to |
()
|
**kwargs
|
Any
|
Keyword arguments passed to |
{}
|
unestimate ¶
Alias for uncompute(), used when running in symbolic mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to |
()
|
**kwargs
|
Any
|
Keyword arguments passed to |
{}
|
compute ¶
Executes the quantum computation defined by this Qubrick.
This method:
- Calls
_compute(*args, **kwargs)to apply the main quantum logic. - Manages temporary qubit registers automatically.
- Handles symbolic computation when used in a resource estimation mode.
For more details, see High-Level Routines: Qubricks tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to |
()
|
**kwargs
|
Any
|
Keyword arguments passed to |
{}
|
Example
parity = Parity()
qc = QPU(num_qubits=5)
reg = Qubits(4, "reg", qc)
parity.compute(reg) # Apply the Qubrick computation
parity.uncompute() # Uncompute it manually
Note:
- If dagger=True, applies the inverse operation.
- If do_gates=False, only visualizes the operation.
- If symbolic mode is enabled, captures computation for analysis.
Best Practice
- Prefer using
computed()context manager when possible for automatic uncompute.
uncompute ¶
Automatically uncomputes the quantum operations applied during compute().
This function attempts to reverse the effects of compute() using one of the following methods:
- Simple Daggering (default): The recorded operations in
compute_opsare reversed and executed. - Custom Uncompute (if
_uncompute()exists): A specialized_uncompute()method is called instead. - Symbolic Uncompute (if
is_symbolic=True): Calls_unestimate()instead of_uncompute(). - Never Uncompute (if
never_uncompute=True): Raises an error if uncompute is called.
For more details, see Uncomputation Using Qubricks tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Deprecated and will be removed in WB 5.0.0 - Uncompute does not accept arguments. |
()
|
**kwargs
|
Any
|
Deprecated and will be removed in WB 5.0.0 - Uncompute does not accept keyword arguments. |
{}
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
RuntimeError
|
If |
Example
parity = Parity()
parity.compute(input_qreg)
parity.uncompute() # This reverses compute() operations
Automatic Uncompute Using computed()
with parity.computed(input_qreg) as result:
result.z() # Uncompute is called automatically when exiting the block
Note
- If
compute()used custom logic,_uncompute()is used instead. - If symbolic mode is active (
is_symbolic=True),_unestimate()is used. - If
never_uncompute=True, an error is raised instead of uncomputing. - If
compute_opsisNone,make_dagger_ops()is used to generate the inverse operations.
release_temp_qubits ¶
Releases temporary qubit registers allocated during compute() and uncompute().
This function: - Ensures that no auxiliary qubits remain allocated after execution. - Releases temporary registers after compute and/or uncompute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
release_compute
|
bool
|
If |
True
|
release_uncompute
|
bool
|
If |
True
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called before |
Notes - This function prevents qubit memory leaks. - If a qubit is still allocated at the end, a warning is raised.
computed ¶
Context manager for automatic computation and uncomputation.
When used in a with block, compute() is called upon entry and uncompute() is called upon exit.
For more details, see Uncomputation Context Manager tutorial.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Arguments to pass to |
()
|
**kwargs
|
Any
|
Keyword arguments to pass to |
{}
|
Returns:
| Type | Description |
|---|---|
AutoUncomputeBlock
|
A context manager that automatically uncomputes. |
Example
Note
- Preferred over manually calling
compute()anduncompute()for better readability and safety.
QubrickComputeState Class¶
psiqworkbench.qubricks.qubrick.QubrickComputeState ¶
Stores compute state information for a Qubrick instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qbk
|
Qubrick
|
The Qubrick instance associated with this compute state. |
required |
compute_args
|
dict
|
Arguments passed to |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
verbose |
bool
|
If |
qbk |
Qubrick
|
The |
qc |
QPU
|
The associated quantum processing unit. |
compute_args |
dict
|
The arguments passed to |
dagger |
bool
|
Indicates if the Qubrick is daggered (inverse computation). |
temp_qregs |
list[Qubits]
|
List of temporary qubit registers. |
input_qregs |
dict[str, Qubits]
|
Dictionary mapping input register names to Qubits. |
result_qregs |
dict[str, Qubits]
|
Dictionary storing result qubit registers. |
result_qregs_by_alloc_name |
dict[str, Qubits]
|
Maps allocated register names to result qubits. |
classical_results |
dict[str, Any]
|
Dictionary storing classical results (e.g. measurements). |
compute_ops |
list
|
Stores quantum operations applied during |
dagger_ops |
list
|
Stores daggered (inverse) operations. |
compute_iterations |
int
|
Number of times |
debug_errors |
list[dict]
|
Stores debugging error messages with associated registers. |
cs_key |
int
|
Unique identifier for this compute state, assigned by the QPU. |
debug_errors |
list[dict]
|
Stores recorded debug errors encountered during execution. |
result_qregs_by_alloc_name |
dict[str, Qubits]
|
Maps allocated register names to result registers. |
compute_iterations |
int
|
The number of times the |
symbolics |
bool
|
If |
temp_qregs |
list[Qubits]
|
A list of temporary quantum registers allocated during computation. |
input_qregs |
dict[str, Qubits]
|
Dictionary storing the input quantum registers. |
classical_results |
dict[str, Any]
|
Stores classical results associated with the computation. |
debug_errors |
list[dict]
|
A list of dictionaries recording debug errors and messages. |
compute_ops |
list
|
The quantum operations executed during |
dagger_ops |
list
|
The corresponding dagger (inverse) operations for |
cs_key |
int
|
A unique key assigned to track compute state in the QPU. |
Note
- Unallocated temporary registers are cleared before usage.
cs_keyis used to track compute state events within the QPU.
is_symbolic
property
¶
Indicates whether QubrickComputeState operates in symbolic mode.
temp_qreg ¶
Retrieves a temporary qubit register by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the temporary qubit register. |
required |
Returns:
| Type | Description |
|---|---|
Qubits or list[Qubits]
|
|
qreg ¶
Retrieves a qubit register by name, searching both temporary and input registers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the register to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Qubits or None
|
|
get_all_qregs ¶
Retrieves all qubit registers used in the computation.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing:
|
set_debug_error ¶
Records an arithmetic error for debugging purposes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regs
|
list of Qubits
|
The registers involved in the error. |
required |
message
|
str
|
A description of the problem. |
required |
do_warning
|
bool
|
If |
True
|
Note
- Errors are stored inside the
QubrickComputeStatefor debugging. - If
do_warning=True, a warning is printed immediately.
get_debug_errors ¶
Retrieves a list of recorded debug errors.
Returns:
| Type | Description |
|---|---|
list
|
A list of dictionaries containing:
|
Note
- This function is used for debugging arithmetic errors.
alloc_temp_qreg ¶
Allocates a temporary qubit register for use in compute().
This function:
- Allocates a new Qubits register if not in symbolic mode.
- Allocates a SymbolicQubits register if
is_symbolic=True. - Handles automatic qubit release based on
release_after_computeandrelease_after_uncompute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_qubits
|
int or Parameter
|
Number of qubits to allocate. |
required |
name
|
str
|
The name of the allocated register. |
required |
release_after_compute
|
bool
|
If |
False
|
release_after_uncompute
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
Qubits or SymbolicQubits
|
The allocated qubit register. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
set_result_qreg ¶
Stores a Qubits-based computation result for later retrieval.
This function:
- Assigns a result register that can be accessed after computation.
- Tracks the register in
self.result_qregsby name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qreg
|
Qubits or None
|
The qubit register to store as a result. |
required |
name
|
str
|
The name to assign to the result. Default is |
'result'
|
get_result_qreg ¶
Retrieves a Qubits-based result register stored during computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the result register to retrieve. Default is |
'result'
|
Returns:
| Type | Description |
|---|---|
Qubits or None
|
The stored result register, or |
set_classical_result ¶
Stores a classical computation result for retrieval.
This function:
- Assigns a classical result under a specific name.
- Useful for storing measurement results or classical values.
- Complements
get_classical_result()for retrieval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
The classical value to store. |
required |
name
|
str
|
The name under which to store the result. Default is |
'result'
|
Note
- If a classical result with the same name already exists, it is overwritten.
get_classical_result ¶
Retrieves a named classical result stored during computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the result parameter. Default is |
'result'
|
Returns:
| Type | Description |
|---|---|
Any
|
The stored classical result, or |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called before |
Note
- If no result has been explicitly set, it falls back to the compute arguments.
- Complements
set_classical_result()for storing and retrieving classical data.