netqasm.sdk.builder

Conversion from Python code into an NetQASM subroutines.

This module contains the Builder class, which is used by a Connection to transform Python application script code into NetQASM subroutines.

class netqasm.sdk.builder.LabelManager

Bases: object

Simple manager class for providing unique branch labels.

new_label(start_with='')
Parameters:

start_with (str)

Return type:

str

class netqasm.sdk.builder.SdkIfContext(id, builder, condition, op0, op1)

Bases: object

Context object for if statements in SDK code such as with conn.if_eq().

Parameters:
class netqasm.sdk.builder.SdkForEachContext(id, builder, array, return_index)

Bases: object

Context object for foreach statements in SDK code such as with conn.foreach().

Parameters:
  • id (int)

  • builder (Builder)

  • array (Array)

  • return_index (bool)

class netqasm.sdk.builder.SdkLoopUntilContext(id, builder, max_iterations)

Bases: object

Context object for loop_until() statements in SDK code.

Parameters:
  • id (int)

  • builder (Builder)

  • max_iterations (int)

set_exit_condition(constraint)

Set the exit condition for this while loop.

Parameters:

constraint (SdkConstraint)

Return type:

None

property exit_condition: SdkConstraint | None

Get the exit condition for this while loop.

set_cleanup_code(cleanup_code)

Set the cleanup code for this while loop.

Parameters:

cleanup_code (T_CleanupRoutine)

Return type:

None

property cleanup_code: T_CleanupRoutine | None

Get the cleanup code for this while loop.

set_loop_register(register)

Set the register used that holds the iteration index for this while loop.

Parameters:

register (RegFuture)

Return type:

None

property loop_register: RegFuture | None

Get the register used that holds the iteration index for this while loop.

property max_iterations: int

Get the maximum number of iterations for this while loop.

class netqasm.sdk.builder.Builder(connection, app_id, hardware_config=None, log_config=None, compiler=None, return_arrays=True)

Bases: object

Object that transforms Python script code into `ProtoSubroutine`s.

A Connection uses a Builder to handle statements in application script code. The Builder converts the statements into pseudo-NetQASM instructions that are assembled into a ProtoSubroutine. When the connectin flushes, the ProtoSubroutine is is compiled into a NetQASM subroutine.

Parameters:
  • connection (BaseNetQASMConnection)

  • app_id (int)

  • hardware_config (Optional[HardwareConfig])

  • log_config (Optional[LogConfig])

  • compiler (Optional[Type[SubroutineTranspiler]])

  • return_arrays (bool)

__init__(connection, app_id, hardware_config=None, log_config=None, compiler=None, return_arrays=True)

Builder constructor. Typically not used directly by the Host script.

Parameters:
  • connection (BaseNetQASMConnection) – Connection that this builder builds for

  • app_id (int) – ID of the application as given by the quantum node controller

  • max_qubits – maximum number of qubits allowed (as registered with the quantum node controller)

  • log_config (Optional[LogConfig]) – logging configuration, typically just passed as-is by the connection object

  • compiler (Optional[Type[SubroutineTranspiler]]) – which compiler class to use for the translation from ProtoSubroutine to Subroutine

  • return_arrays (bool) – whether to add ret_arr NetQASM instructions at the end of each subroutine (for all arrays that are used in the subroutine). May be set to False if the quantum node controller does not support returning arrays.

  • hardware_config (Optional[HardwareConfig])

property app_id: int
inactivate_qubits()
Return type:

None

new_qubit_id()
Return type:

int

alloc_array(length=1, init_values=None)
Parameters:
  • length (int)

  • init_values (Optional[List[Optional[int]]])

Return type:

Array

new_register(init_value=0)
Parameters:

init_value (int)

Return type:

RegFuture

subrt_add_pending_commands(commands)
Parameters:

commands (List[Union[ICmd, BranchLabel]])

Return type:

None

subrt_add_pending_command(command)
Parameters:

command (Union[ICmd, BranchLabel])

Return type:

None

subrt_pop_all_pending_commands()
Return type:

List[Union[ICmd, BranchLabel]]

subrt_pop_pending_subroutine()
Return type:

Optional[ProtoSubroutine]

subrt_compile_subroutine(pre_subroutine)

Convert a ProtoSubroutine into a Subroutine.

Parameters:

pre_subroutine (ProtoSubroutine)

Return type:

Subroutine

property committed_subroutines: List[Subroutine]
if_context_enter(context_id)
Parameters:

context_id (int)

Return type:

None

if_context_exit(context_id, condition, op0, op1)
Parameters:
Return type:

None

sdk_epr_keep(role, params, reset_results_array=False)

Build commands for an EPR keep operation and return the result futures.

Parameters:
  • role (EPRRole)

  • params (EntRequestParams)

  • reset_results_array (bool)

Return type:

Tuple[List[Qubit], Array]

sdk_epr_measure(role, params)

Build commands for an EPR measure operation and return the result futures.

Parameters:
  • role (EPRRole)

  • params (EntRequestParams)

Return type:

List[EprMeasureResult]

sdk_epr_rsp_create(params)

Build commands for a ‘create remote state preparation’ EPR operation and return the result futures.

Parameters:

params (EntRequestParams)

Return type:

List[EprMeasureResult]

sdk_epr_rsp_recv(params)

Build commands for a ‘receive remote state preparation’ EPR operation and return the created qubits and result futures.

Parameters:

params (EntRequestParams)

Return type:

Tuple[List[Qubit], List[EprKeepResult]]

sdk_create_epr_keep(params)

Build commands for a ‘create and keep’ EPR operation and return the created qubits and result futures.

Parameters:

params (EntRequestParams)

Return type:

Tuple[List[Qubit], List[EprKeepResult]]

sdk_recv_epr_keep(params)

Build commands for a ‘receive and keep’ EPR operation and return the created qubits and result futures.

Parameters:

params (EntRequestParams)

Return type:

Tuple[List[Qubit], List[EprKeepResult]]

sdk_create_epr_measure(params)

Build commands for a ‘create and measure’ EPR operation and return the result futures.

Parameters:

params (EntRequestParams)

Return type:

List[EprMeasureResult]

sdk_recv_epr_measure(params)

Build commands for a ‘receive and measure’ EPR operation and return the result futures.

Parameters:

params (EntRequestParams)

Return type:

List[EprMeasureResult]

sdk_create_epr_rsp(params)

Build commands for a ‘create remote state preperation’ EPR operation and return the result futures.

Parameters:

params (EntRequestParams)

Return type:

List[EprMeasureResult]

sdk_recv_epr_rsp(params)

Build commands for a ‘receive remote state preparation’ EPR operation and return the created qubits and result futures.

Parameters:

params (EntRequestParams)

Return type:

Tuple[List[Qubit], List[EprKeepResult]]

sdk_loop_context(stop, start=0, step=1, loop_register=None)

Build commands for a ‘loop’ context and return the context object.

Parameters:
  • stop (int)

  • start (int)

  • step (int)

  • loop_register (Union[Register, str, None])

Return type:

Iterator[Register]

sdk_loop_body(body, stop, start=0, step=1, loop_register=None)

Build commands for looping the code in the specified body.

Parameters:
  • body (T_LoopRoutine)

  • stop (int)

  • start (int)

  • step (int)

  • loop_register (Optional[Union[operand.Register, str]])

Return type:

None

sdk_if_eq(op0, op1, body)

An effective if-statement where body is a function executing the clause for a == b

Parameters:
  • op0 (T_CValue)

  • op1 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_if_ne(op0, op1, body)

An effective if-statement where body is a function executing the clause for a != b

Parameters:
  • op0 (T_CValue)

  • op1 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_if_lt(op0, op1, body)

An effective if-statement where body is a function executing the clause for a < b

Parameters:
  • op0 (T_CValue)

  • op1 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_if_ge(op0, op1, body)

An effective if-statement where body is a function executing the clause for a >= b

Parameters:
  • op0 (T_CValue)

  • op1 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_if_ez(op0, body)

An effective if-statement where body is a function executing the clause for a == 0

Parameters:
  • op0 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_if_nz(op0, body)

An effective if-statement where body is a function executing the clause for a != 0

Parameters:
  • op0 (T_CValue)

  • body (T_BranchRoutine)

Return type:

None

sdk_new_if_context(condition, op0, op1)

Build commands for an ‘if’ context and return the context object.

Parameters:
Return type:

SdkIfContext

sdk_new_foreach_context(array, return_index)

Build commands for an ‘foreach’ context and return the context object.

Parameters:
  • array (Array)

  • return_index (bool)

Return type:

SdkForEachContext

sdk_new_loop_until_context(max_iterations)

Build commands for a ‘loop_until’ context and return the context object.

Parameters:

max_iterations (int)

Return type:

Iterator[SdkLoopUntilContext]

sdk_try_context(max_tries=1)

Build commands for a ‘try’ context.

Parameters:

max_tries (int)

Return type:

Iterator[None]

sdk_create_epr_context(params)

Build commands for an EPR context and return an iterator over the EPR qubits and indices created in this context.

Parameters:

params (EntRequestParams)

Return type:

Iterator[Tuple[FutureQubit, RegFuture]]