netqasm.sdk.qubit

Qubit representation.

This module contains the Qubit class, which are used by application scripts as handles to in-memory qubits.

exception netqasm.sdk.qubit.QubitNotActiveError

Bases: MemoryError

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class netqasm.sdk.qubit.QubitMeasureBasis(value)

Bases: enum.Enum

An enumeration.

X = 0
Y = 1
Z = 2
class netqasm.sdk.qubit.Qubit(conn, add_new_command=True, ent_info=None, virtual_address=None)

Bases: object

Representation of a qubit that has been allocated in the quantum node.

A Qubit instance represents a quantum state that is stored in a physical qubit somewhere in the quantum node. The particular qubit is identified by its virtual qubit ID. To which physical qubit ID this is mapped (at a given time), is handled completely by the quantum node controller and is not known to the Qubit itself.

A Qubit object can be instantiated in an application script. Such an instantiation is automatically compiled into NetQASM instructions that allocate and initialize a new qubit in the quantum node controller.

A Qubit object may also be obtained by SDK functions that return them, like the create() method on an EPRSocket, which returns the object as a handle to the qubit that is now entangled with one in another node.

Qubit operations like applying gates and measuring them are done by calling methods on a Qubit instance.

Parameters
  • conn (sdkconn.BaseNetQASMConnection) –

  • add_new_command (bool) –

  • ent_info (Optional[qlink_compat.LinkLayerOKTypeK]) –

  • virtual_address (Optional[int]) –

__init__(conn, add_new_command=True, ent_info=None, virtual_address=None)

Qubit constructor. This is the standard way to allocate a new qubit in an application.

Parameters
  • conn (sdkconn.BaseNetQASMConnection) – connection of the application in which to allocate the qubit

  • add_new_command (bool) – whether to automatically add NetQASM instructions to the current subroutine to allocate and initialize the qubit

  • ent_info (Optional[qlink_compat.LinkLayerOKTypeK]) – entanglement generation information in case this qubit is the result of an entanglement generation request

  • virtual_address (Optional[int]) – explicit virtual ID to use for this qubit. If None, a free ID is automatically chosen.

property connection

Get the NetQASM connection of this qubit

Return type

sdkconn.BaseNetQASMConnection

property builder

Get the Builder of this qubit’s connection

Return type

Builder

property qubit_id

Get the qubit ID

Return type

int

property active
Return type

bool

property entanglement_info

Get information about the successful link layer request that resulted in this qubit.

Return type

Optional[qlink_compat.LinkLayerOKTypeK]

property remote_entangled_node

Get the name of the remote node the qubit is entangled with.

If not entanled, None is returned.

Return type

Optional[str]

assert_active()

Assert that the qubit is active, i.e. allocated.

Return type

None

measure(future=None, inplace=False, store_array=True, basis=<QubitMeasureBasis.Z: 2>, basis_rotations=None)

Measure the qubit in the standard basis and get the measurement outcome.

Parameters
  • future (Union[Future, RegFuture, None]) – the Future to place the outcome in. If None, a Future is created automatically.

  • inplace (bool) – If False, the measurement is destructive and the qubit is removed from memory. If True, the qubit is left in the post-measurement state.

  • store_array (bool) – whether to store the outcome in an array. If not, it is placed in a register. Only used if future is None.

  • basis (QubitMeasureBasis) – in which of the Pauli bases (X, Y or Z) to measure. Default is Z. Ignored if basis_rotations is not None.

  • basis_rotations (Optional[Tuple[int, int, int]]) – rotations to apply before measuring in the Z-basis. This can be used to specify arbitrary measurement bases. The 3 values are interpreted as 3 rotation angles, for an X- Y-, and another X-rotation, respectively. Each angle is interpreted as a multiple of pi/16. For example, if basis_rotations is (8, 0, 0), an X-rotation is applied with angle 8*pi/16 = pi/2 radians, followed by a Y-rotation of angle 0 and an X-rotation of angle 0. Finally, the measurement is done in the Z-basis.

Return type

Union[Future, RegFuture]

Returns

the Future representing the measurement outcome. It is a Future if

the result is in an array (default) or RegFuture if the result is in a register.

X()

Apply an X gate on the qubit.

Return type

None

Y()

Apply a Y gate on the qubit.

Return type

None

Z()

Apply a Z gate on the qubit.

Return type

None

T()

Apply a T gate on the qubit.

A T gate is a Z-rotation with angle pi/4.

Return type

None

H()

Apply a Hadamard gate on the qubit.

Return type

None

K()

Apply a K gate on the qubit.

A K gate moves the |0> state to +|i> (positive Y) and vice versa.

Return type

None

S()

Apply an S gate on the qubit.

An S gate is a Z-rotation with angle pi/2.

Return type

None

rot_X(n=0, d=0, angle=None)

Do a rotation around the X-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

rot_Y(n=0, d=0, angle=None)

Do a rotation around the Y-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

rot_Z(n=0, d=0, angle=None)

Do a rotation around the Z-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

cnot(target)

Apply a CNOT gate between this qubit (control) and a target qubit.

Parameters

target (Qubit) – target qubit. Should have the same connection as this qubit.

Return type

None

cphase(target)

Apply a CPHASE (CZ) gate between this qubit (control) and a target qubit.

Parameters

target (Qubit) – target qubit. Should have the same connection as this qubit.

Return type

None

reset()

Reset the qubit to the state |0>.

Return type

None

free()

Free the qubit and its virtual ID.

After freeing, the underlying physical qubit can be used to store another state.

Return type

None

class netqasm.sdk.qubit.FutureQubit(conn, future_id)

Bases: netqasm.sdk.qubit.Qubit

A Qubit that will be available in the future.

This class is very similar to the Future class which is used for classical values. A FutureQubit acts like a Qubit so that all qubit operations can be applied on it. FutureQubits are typically the result of EPR creating requests, where they represent the qubits that will be available when EPR generation has finished.

Parameters
  • conn (sdkconn.BaseNetQASMConnection) –

  • future_id (Future) –

__init__(conn, future_id)

FutureQubit constructor. Typically not used directly.

Parameters
  • conn (sdkconn.BaseNetQASMConnection) – connection through which subroutines are sent that contain this qubit

  • future_id (Future) – the virtual ID this qubit will have

H()

Apply a Hadamard gate on the qubit.

Return type

None

K()

Apply a K gate on the qubit.

A K gate moves the |0> state to +|i> (positive Y) and vice versa.

Return type

None

S()

Apply an S gate on the qubit.

An S gate is a Z-rotation with angle pi/2.

Return type

None

T()

Apply a T gate on the qubit.

A T gate is a Z-rotation with angle pi/4.

Return type

None

X()

Apply an X gate on the qubit.

Return type

None

Y()

Apply a Y gate on the qubit.

Return type

None

Z()

Apply a Z gate on the qubit.

Return type

None

property active
Return type

bool

assert_active()

Assert that the qubit is active, i.e. allocated.

Return type

None

property builder

Get the Builder of this qubit’s connection

Return type

Builder

cnot(target)

Apply a CNOT gate between this qubit (control) and a target qubit.

Parameters

target (Qubit) – target qubit. Should have the same connection as this qubit.

Return type

None

property connection

Get the NetQASM connection of this qubit

Return type

sdkconn.BaseNetQASMConnection

cphase(target)

Apply a CPHASE (CZ) gate between this qubit (control) and a target qubit.

Parameters

target (Qubit) – target qubit. Should have the same connection as this qubit.

Return type

None

free()

Free the qubit and its virtual ID.

After freeing, the underlying physical qubit can be used to store another state.

Return type

None

measure(future=None, inplace=False, store_array=True, basis=<QubitMeasureBasis.Z: 2>, basis_rotations=None)

Measure the qubit in the standard basis and get the measurement outcome.

Parameters
  • future (Union[Future, RegFuture, None]) – the Future to place the outcome in. If None, a Future is created automatically.

  • inplace (bool) – If False, the measurement is destructive and the qubit is removed from memory. If True, the qubit is left in the post-measurement state.

  • store_array (bool) – whether to store the outcome in an array. If not, it is placed in a register. Only used if future is None.

  • basis (QubitMeasureBasis) – in which of the Pauli bases (X, Y or Z) to measure. Default is Z. Ignored if basis_rotations is not None.

  • basis_rotations (Optional[Tuple[int, int, int]]) – rotations to apply before measuring in the Z-basis. This can be used to specify arbitrary measurement bases. The 3 values are interpreted as 3 rotation angles, for an X- Y-, and another X-rotation, respectively. Each angle is interpreted as a multiple of pi/16. For example, if basis_rotations is (8, 0, 0), an X-rotation is applied with angle 8*pi/16 = pi/2 radians, followed by a Y-rotation of angle 0 and an X-rotation of angle 0. Finally, the measurement is done in the Z-basis.

Return type

Union[Future, RegFuture]

Returns

the Future representing the measurement outcome. It is a Future if

the result is in an array (default) or RegFuture if the result is in a register.

property qubit_id

Get the qubit ID

Return type

int

reset()

Reset the qubit to the state |0>.

Return type

None

rot_X(n=0, d=0, angle=None)

Do a rotation around the X-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

rot_Y(n=0, d=0, angle=None)

Do a rotation around the Y-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

rot_Z(n=0, d=0, angle=None)

Do a rotation around the Z-axis of the specified angle.

The angle is interpreted as ǹ * pi / 2 ^d` radians. For example, (n, d) = (1, 2) represents an angle of pi/4 radians. If angle is specified, n and d are ignored and this instruction is automatically converted into a sequence of (n, d) rotations such that the discrete (n, d) values approximate the original angle.

Parameters
  • n (Union[int, Template]) – numerator of discrete angle specification. Can be a Template, in which case the subroutine containing this command should first be instantiated before flushing.

  • d (int) – denomerator of discrete angle specification

  • angle (Optional[float]) – exact floating-point angle, defaults to None

property entanglement_info

Get information about the successful link layer request that resulted in this qubit.

Return type

Optional[qlink_compat.LinkLayerOKTypeK]

property remote_entangled_node

Get the name of the remote node the qubit is entangled with.

If not entanled, None is returned.

Return type

Optional[str]