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:
IntEnumAn enumeration.
- X = 0
- Y = 1
- Z = 2
- conjugate()
Returns self, the complex conjugate of any int.
- bit_length()
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- bit_count()
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- to_bytes(length, byteorder, *, signed=False)
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- from_bytes(byteorder, *, signed=False)
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
- signed
Indicates whether two’s complement is used to represent the integer.
- as_integer_ratio()
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int and with a positive denominator.
>>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1)
- real
the real part of a complex number
- imag
the imaginary part of a complex number
- numerator
the numerator of a rational number in lowest terms
- denominator
the denominator of a rational number in lowest terms
- class netqasm.sdk.qubit.QubitMeasureAxes(value)
Bases:
IntEnumAn enumeration.
- XYX = 0
- YZY = 1
- ZXZ = 2
- conjugate()
Returns self, the complex conjugate of any int.
- bit_length()
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- bit_count()
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- to_bytes(length, byteorder, *, signed=False)
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- from_bytes(byteorder, *, signed=False)
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value.
- signed
Indicates whether two’s complement is used to represent the integer.
- as_integer_ratio()
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int and with a positive denominator.
>>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1)
- real
the real part of a complex number
- imag
the imaginary part of a complex number
- numerator
the numerator of a rational number in lowest terms
- denominator
the denominator of a rational number in lowest terms
- class netqasm.sdk.qubit.Qubit(conn, add_new_command=True, ent_info=None, virtual_address=None)
Bases:
objectRepresentation 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 (
BaseNetQASMConnection)add_new_command (
bool)ent_info (
Optional[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 (
BaseNetQASMConnection) – connection of the application in which to allocate the qubitadd_new_command (
bool) – whether to automatically add NetQASM instructions to the current subroutine to allocate and initialize the qubitent_info (
Optional[LinkLayerOKTypeK]) – entanglement generation information in case this qubit is the result of an entanglement generation requestvirtual_address (
Optional[int]) – explicit virtual ID to use for this qubit. If None, a free ID is automatically chosen.
- property connection: BaseNetQASMConnection
Get the NetQASM connection of this qubit
- property qubit_id: int
Get the qubit ID
- property active: bool
- property entanglement_info: LinkLayerOKTypeK | None
Get information about the successful link layer request that resulted in this qubit.
- property remote_entangled_node: str | None
Get the name of the remote node the qubit is entangled with.
If not entanled, None is returned.
- 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, basis_rotations=None, basis_rotation_axes=QubitMeasureAxes.XYX)
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, around the axes specified by basis_rotation_axes. Each angle is interpreted as a multiple of pi/16. For example, if basis_rotations is (8, 0, 0) and basis_rotation_axes is XYX, 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.basis_rotation_axes (
QubitMeasureAxes) – Axes around which the rotations from basis_rotations should be performed, in order.
- Return type:
- 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 specificationangle (
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 specificationangle (
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 specificationangle (
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
- swap(target)
Swap the state of the qubit with the state of another qubit.
- Parameters:
target (
Qubit) – target qubit to swap states with.- Return type:
None
- reset()
Reset the qubit to the state |0>.
- Return type:
None
- free(deactivate=True)
Free the qubit and its virtual ID.
After freeing, the underlying physical qubit can be used to store another state.
- Parameters:
deactivate (
bool)- Return type:
None
- class netqasm.sdk.qubit.FutureQubit(conn, future_id)
Bases:
QubitA 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 (
BaseNetQASMConnection)future_id (
Future)
- __init__(conn, future_id)
FutureQubit constructor. Typically not used directly.
- Parameters:
conn (
BaseNetQASMConnection) – connection through which subroutines are sent that contain this qubitfuture_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: bool
- assert_active()
Assert that the qubit is active, i.e. allocated.
- Return type:
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
- property connection: BaseNetQASMConnection
Get the NetQASM connection of this qubit
- 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(deactivate=True)
Free the qubit and its virtual ID.
After freeing, the underlying physical qubit can be used to store another state.
- Parameters:
deactivate (
bool)- Return type:
None
- measure(future=None, inplace=False, store_array=True, basis=QubitMeasureBasis.Z, basis_rotations=None, basis_rotation_axes=QubitMeasureAxes.XYX)
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, around the axes specified by basis_rotation_axes. Each angle is interpreted as a multiple of pi/16. For example, if basis_rotations is (8, 0, 0) and basis_rotation_axes is XYX, 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.basis_rotation_axes (
QubitMeasureAxes) – Axes around which the rotations from basis_rotations should be performed, in order.
- Return type:
- 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: int
Get the qubit ID
- 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 specificationangle (
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 specificationangle (
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 specificationangle (
Optional[float]) – exact floating-point angle, defaults to None
- swap(target)
Swap the state of the qubit with the state of another qubit.
- Parameters:
target (
Qubit) – target qubit to swap states with.- Return type:
None
- property entanglement_info: LinkLayerOKTypeK | None
Get information about the successful link layer request that resulted in this qubit.
- property remote_entangled_node: str | None
Get the name of the remote node the qubit is entangled with.
If not entanled, None is returned.