isqtools.backend package

Submodules

isqtools.backend.abstract_backend module

Abstract backend.

class AbstractBackend

Bases: ABC

Abstract backend to run quantum circuits. This is the basis for all simulation and hardware methods.

abstractmethod probs()

This method can get all the probability distributions after the measurement of the specified qubit. The probability distribution obtained by this method is the theoretical value after the state vector simulation, and shots are not considered. Therefore, this method can only be used for circuit simulation and cannot be used for hardware backends.

Return type:

Any

Returns:

Tensor(array) distribution. Sort by the size of the binary representation of the quantum state.

abstractmethod sample()

This method can get the sampling result of the specified qubit measurement. Unlike the probs method, this method obtains the measurement results of the quantum circuit by sampling, and the results are related to shots. This method can be implemented using a simulator or the specific hardware.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

isqtools.backend.arclight_backend module

Arclight quantum cloud backend. For more information, check out: http://qcloud.arclightquantum.com/#/home

class ArclightBackend(token=None, version=None, sleep_time=3, run_time=None, mapping=False)

Bases: HardwareBackend

The backend of arclighth quantum cloud.

Parameters:
  • token (str | None)

  • version (str | None)

  • sleep_time (int)

  • run_time (int | None)

  • mapping (bool)

sample(data, shots=100, **kwargs)

Call quantum hardware for sampling.

Parameters:
  • data (str) – QCIS strings, This represents the information of the quantum circuit.

  • shots (int) – Shots numbers.

  • **kwargs – Arbitrary keyword arguments.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

isqtools.backend.autograd_backend module

Autograd backend. Autograd can automatically differentiate native Python and Numpy code. It can handle a large subset of Python’s features, including loops, ifs, recursion and closures, and it can even take derivatives of derivatives of derivatives. It supports reverse-mode differentiation (a.k.a. backpropagation), which means it can efficiently take gradients of scalar-valued functions with respect to array-valued arguments, as well as forward-mode differentiation, and the two can be composed arbitrarily. The main intended application of Autograd is gradient-based optimization. For more information, check out: https://github.com/HIPS/autograd

class AutogradBackend

Bases: StateVectorSimulator

This method inherits the StateVectorSimulator, and uses autograd to perform the mathematical calculation.

H()
Return type:

Any

RX(theta)
Parameters:

theta (Any)

Return type:

Any

RXY(theta, phi)
Parameters:
  • theta (Any)

  • phi (Any)

Return type:

Any

RY(theta)
Parameters:

theta (Any)

Return type:

Any

RZ(theta)
Parameters:

theta (Any)

Return type:

Any

S()
Return type:

Any

SD()
Return type:

Any

T()
Return type:

Any

TD()
Return type:

Any

X()
Return type:

Any

X2M()
Return type:

Any

X2P()
Return type:

Any

Y()
Return type:

Any

Y2M()
Return type:

Any

Y2P()
Return type:

Any

Z()
Return type:

Any

as_tensor(tensor)

Convert to the corresponding tensor.

Parameters:

tensor (Any)

Return type:

Any

conj(state)
Parameters:

state (Any)

Return type:

Any

copy(state)
Parameters:

state (Any)

Return type:

Any

dot(state1, state2)
Parameters:
  • state1 (Any)

  • state2 (Any)

Return type:

Any

get_zero_state(qnum)
Parameters:

qnum (int)

Return type:

Any

ravel(state)
Parameters:

state (Any)

Return type:

Any

real(state)
Parameters:

state (Any)

Return type:

Any

reshape(state, shape)
Parameters:
  • state (Any)

  • shape (Sequence[int])

Return type:

Any

sqrt(state)
Parameters:

state (Any)

Return type:

Any

stack(states, axis=0)
Parameters:
  • states (Sequence[Any])

  • axis (int)

Return type:

Any

sum(state, axis=None)
Parameters:
  • state (Any)

  • axis (int | None)

Return type:

Any

isqtools.backend.hardware_backend module

Quantum hardware operations are performed by submitting the QCIS instruction set. This file is the basis for hardware.

class HardwareBackend

Bases: AbstractBackend

Hardware running base.

probs()

Running a circuit on hardware is different from a simulator, and it is impossible to obtain an accurate probability distribution.

Raises:
Return type:

NoReturn

sample(data, shots=100, **kwargs)

Sampling quantum circuits using hardware. This is an abstraction over the concrete method.

Parameters:
  • data (str) – QCIS strings, This represents the information of the quantum circuit.

  • shots (int) – Shots numbers.

  • **kwargs – Arbitrary keyword arguments.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

exception HardwareBackendError

Bases: Exception

Error for hardware running.

class ISQTask(task_id, task_type, state, device, shots, **kwargs)

Bases: object

isq hardware task interface, used to execute hardware tasks.

Parameters:
  • task_id (int | str)

  • task_type (str)

  • state (str)

  • device (HardwareBackend)

  • shots (int)

NO_RESULT_TERMINAL_STATES = {'CANCELLED', 'FAILED'}
result()

Query the result of the task according to the id of the task.

Return type:

dict

Returns:

This method returns a dictionary, which is the same fotmat as sample.

property state: str

Get the status of the task.

Returns:

  1. WAITING.

  2. COMPLETED.

  3. FAILED.

  4. CANCELLED.

Return type:

Returns four different states

class TaskState

Bases: object

Types of hardware tasks.

CANCEL = 'CANCELLED'
COMPLETE = 'COMPLETED'
FAIL = 'FAILED'
WAIT = 'WAITING'
class TaskType

Bases: object

Type of hardware.

ARCLIGHT = 'ARCLIGHT'
QCIS = 'QCIS'
QCIS2 = 'QCIS2'

isqtools.backend.numpy_backend module

Numpy backend. NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more. For more information, check out: https://numpy.org/

class NumpyBackend

Bases: StateVectorSimulator

This method inherits the StateVectorSimulator, and uses autograd to perform the mathematical calculation.

H()
Return type:

Any

RX(theta)
Parameters:

theta (Any)

Return type:

Any

RXY(theta, phi)
Parameters:
  • theta (Any)

  • phi (Any)

Return type:

Any

RY(theta)
Parameters:

theta (Any)

Return type:

Any

RZ(theta)
Parameters:

theta (Any)

Return type:

Any

S()
Return type:

Any

SD()
Return type:

Any

T()
Return type:

Any

TD()
Return type:

Any

X()
Return type:

Any

X2M()
Return type:

Any

X2P()
Return type:

Any

Y()
Return type:

Any

Y2M()
Return type:

Any

Y2P()
Return type:

Any

Z()
Return type:

Any

as_tensor(tensor)
Parameters:

tensor (Any)

Return type:

Any

conj(state)
Parameters:

state (Any)

Return type:

Any

copy(state)
Parameters:

state (Any)

Return type:

Any

dot(state1, state2)
Parameters:
  • state1 (Any)

  • state2 (Any)

Return type:

Any

get_zero_state(qnum)
Parameters:

qnum (int)

Return type:

Any

ravel(state)
Parameters:

state (Any)

Return type:

Any

real(state)
Parameters:

state (Any)

Return type:

Any

reshape(state, shape)
Parameters:
  • state (Any)

  • shape (Sequence)

Return type:

Any

sqrt(state)
Parameters:

state (Any)

Return type:

Any

stack(states, axis=0)
Parameters:
  • states (Sequence[Any])

  • axis (int)

Return type:

Any

sum(state, axis=None)
Parameters:
  • state (Any)

  • axis (int | None)

Return type:

Any

isqtools.backend.qcis_backend module

Qcis hardware backend. For more information, check out: https://quantumctek-cloud.com/

class QcisBackend(login_key, machine_name, lab_id=None, exp_name=None, remark='arclight', max_wait_time=60, sleep_time=3, run_time=720, mapping=False, is_prob='0', logger=None)

Bases: HardwareBackend

the backend of qcis quantum hardware

Parameters:
  • login_key (str)

  • machine_name (str)

  • lab_id (int | None)

  • exp_name (str | None)

  • remark (str)

  • max_wait_time (int)

  • sleep_time (int)

  • run_time (int | None)

  • mapping (bool)

  • is_prob (bool|str, optional)

  • is_prob – ‘0’ return probability, ‘1’ return sample,

  • sample. ('2' return raw binary string. Defaults to False)

  • login_key

  • machine_name

  • lab_id

  • exp_name

  • remark

  • max_wait_time

  • sleep_time

  • run_time

  • mapping

sample(data, shots=100, **kwargs)

Call quantum hardware for sampling.

Parameters:
  • data (str) – QCIS strings, This represents the information of the quantum circuit.

  • shots (int) – Shots numbers.

  • **kwargs – Arbitrary keyword arguments.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

exception QcisBackendError

Bases: Exception

Qcis backend error

get_rand_str()

Generate a cryptographically secure random string.

Return type:

str

load_params(qcis, **kw)

Use the periodicity to convert the angles in all rotation operations to the range of -2pi to 2pi.

Parameters:

qcis (str) – qcis for quantum circuits.

Return type:

str

Returns:

qcis being converted.

split_rotation_gates(qcis)

The qcis hardware currently has certain limitations. For the rotation operation, when the angle is in the range of -2pi to -pi and pi to 2pi, it is necessary to divide the angle into two and convert it into two rotation operations.

Parameters:

qcis (str) – qcis for quantum circuits.

Return type:

str

Returns:

qcis being converted.

isqtools.backend.qcis_backend2 module

Qcis2 hardware backend. For more information, check out: https://quantumcomputer.ac.cn/

class QcisBackend2(login_key, machine_name, lab_id=None, exp_name=None, max_wait_time=60, sleep_time=3, run_time=None, mapping=False, is_prob=False, logger=None)

Bases: HardwareBackend

the backend of qcis quantum hardware

Parameters:
  • login_key (str)

  • machine_name (str)

  • lab_id (str | None)

  • exp_name (str | None)

  • max_wait_time (int)

  • sleep_time (int)

  • run_time (int | None)

  • mapping (bool)

  • is_prob (bool)

sample(data, shots=100, **kwargs)

Call quantum hardware for sampling.

Parameters:
  • data (str) – QCIS strings, This represents the information of the quantum circuit.

  • shots (int) – Shots numbers.

  • **kwargs – Arbitrary keyword arguments.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

exception QcisBackend2Error

Bases: Exception

Qcis backend error

get_rand_str()

Generate a cryptographically secure random string.

Return type:

str

load_params(qcis, **kw)

Use the periodicity to convert the angles in all rotation operations to the range of -2pi to 2pi.

Parameters:

qcis (str) – qcis for quantum circuits.

Return type:

str

Returns:

qcis being converted.

split_rotation_gates(qcis)

The qcis hardware currently has certain limitations. For the rotation operation, when the angle is in the range of -2pi to -pi and pi to 2pi, it is necessary to divide the angle into two and convert it into two rotation operations.

Parameters:

qcis (str) – qcis for quantum circuits.

Return type:

str

Returns:

qcis being converted.

isqtools.backend.state_vector_simulator module

State vector simulation backend.

class StateVectorSimulator

Bases: AbstractBackend

Simulate quantum circuits using state vector methods. Abstract the mathematical operations required for the simulation process, and the specific implementation methods need to be implemented in its subclasses.

H()
Return type:

Any

RX(theta)
Parameters:

theta (Any)

Return type:

Any

RXY(theta, phi)
Parameters:
  • theta (Any)

  • phi (Any)

Return type:

Any

RY(theta)
Parameters:

theta (Any)

Return type:

Any

RZ(theta)
Parameters:

theta (Any)

Return type:

Any

S()
Return type:

Any

SD()
Return type:

Any

T()
Return type:

Any

TD()
Return type:

Any

X()
Return type:

Any

X2M()
Return type:

Any

X2P()
Return type:

Any

Y()
Return type:

Any

Y2M()
Return type:

Any

Y2P()
Return type:

Any

Z()
Return type:

Any

as_tensor(Tensor)
Return type:

Any

static check(line_data)

Traverse the entire qcis to get circuit information for simulation.

Parameters:

line_data (Iterable) – Each line of the qcis file.

Returns:

The number of qubits required for the circuit. qdic: The number corresponding to the qubit.

Return type:

qnum

Raises:

StateVectorSimulatorError – Check the rationality of the qcis file.

conj(state)
Parameters:

state (Any)

Return type:

Any

copy(state)
Parameters:

state (Any)

Return type:

Any

dot(state1, state2)
Parameters:
  • state1 (Any)

  • state2 (Any)

Return type:

Any

get_zero_state(qnum)
Parameters:

qnum (int)

Return type:

Any

getstate(line_data, qnum, qdic, **kwargs)

Initialize the state vector, traverse the qcis file, apply all gate operations to the state vector, obtain the state after the action, and obtain the number of the measurement qubit.

Parameters:
  • line_data (Iterable) – Each line of the qcis file.

  • qnum (int) – The number of qubits.

  • qdic (dict) – The number corresponding to the qubit.

  • **kwargs – The parameters of the rotation gates.

Returns:

Final state vector. mq: The sequence number of the qubit to be measured.

Return type:

state

multi_gate(state, gate, qnum, ctrl, target)

Using matrix multiplication, multi-qubit gates act on quantum state vectors.

Parameters:
  • state (Any) – Initial state vector before operation.

  • gate (str) – Specific gates acting on the qubit.

  • qnum (int) – Number of qubits.

  • ctrl (int) – The control qubit corresponding to the action.

  • target (int) – The target qubit corresponding to the action.

Return type:

Any

Returns:

Final state vector.

probs(data, **kwargs)

This method can get all the probability distributions after the measurement of the specified qubit. The probability distribution obtained by this method is the theoretical value after the state vector simulation, and shots are not considered.

Parameters:

data (str) – Qcis data.

Return type:

Any

Returns:

Tensor(array) distribution. Sort by the size of the binary.

ravel(state)
Parameters:

state (Any)

Return type:

Any

real(state)
Parameters:

state (Any)

Return type:

Any

reshape(state, shape)
Parameters:
  • state (Any)

  • shape (Sequence)

Return type:

Any

reshape_double(state, qnum, target1, target2)

Before the double-qubit operation is performed, reshape the state vector into the required dimension for matrix multiplication.

Parameters:
  • state (Any) – Initial state vector.

  • qnum (int) – Number of qubits.

  • target1 (int) – The first target qubit corresponding to the action of a double-qubit operation.

  • target2 (int) – The second target qubit corresponding to the action of a double-qubit operation.

Return type:

Any

Returns:

Reshaped state vector.

reshape_single(state, qnum, target)

Before the single-qubit operation is performed, reshape the state vector into the required dimension for matrix multiplication.

Parameters:
  • state (Any) – Initial state vector.

  • qnum (int) – Number of qubits.

  • target (int) – The target qubit corresponding to the action of a single-qubit operation.

Return type:

Any

Returns:

Reshaped state vector.

sample(data, shots=100, rng_seed=None, **kwargs)

This method can get the sampling result of the specified qubit measurement. Unlike the probs method, this method obtains the measurement results of the quantum circuit by sampling, and the results are related to shots.

Return type:

dict[str, int]

Returns:

This method returns a dictionary, the key in the dictionary is the quantum state, and the value is the shots of the quantum state.

Parameters:
  • data (str)

  • shots (int)

  • rng_seed (int | None)

shift(state, qnum, mq)

Qubits are swapped multiple times for probs.

Parameters:
  • state (Any) – State vector before shift.

  • qnum (int) – Number of qubits.

  • mq (Sequence[int]) – The number of qubits to be measured.

Return type:

Any

Returns:

State vector after shift.

single_gate(state, gate, qnum, target)

Using matrix multiplication, single-qubit gates act on quantum state vectors.

Parameters:
  • state (Any) – Initial state vector before operation.

  • gate (str) – Specific gates acting on the qubit.

  • qnum (int) – Number of qubits.

  • target (int) – The target qubit corresponding to the action.

Return type:

Any

Returns:

Final state vector.

single_rotate_gate(state, gate, qnum, target, theta)

Using matrix multiplication, single-qubit-rotate gates act on quantum state vectors.

Parameters:
  • state (Any) – Initial state vector before operation.

  • gate (str) – Specific gates acting on the qubit.

  • qnum (int) – Number of qubits.

  • target (int) – The target qubit corresponding to the action.

  • theta (Any) – Angle of rotation. Extra dimensions after the parameter needed will be discarded.

Return type:

Any

Returns:

Final state vector.

sqrt(state)
Parameters:

state (Any)

Return type:

Any

stack(states, axis=0)
Parameters:
  • states (Sequence[Any])

  • axis (int)

Return type:

Any

state(data, **kwargs)

This method can get the quantum state after operation.

Parameters:

data (str) – Qcis data.

Return type:

Any

Returns:

State as tensor(array).

sum(state, axis=None)
Parameters:
  • state (Any)

  • axis (int | None)

Return type:

Any

swap(state, qnum, q1, q2)

The state vector is swapped, and the sequence number of the qubit is exchanged.

Parameters:
  • state (Any) – State vector before swap.

  • qnum (int) – Number of qubits.

  • q1 (int) – Indexed of qubits to exchange.

  • q2 (int) – Indexed of qubits to exchange.

Return type:

Any

Returns:

State vector after swap.

exception StateVectorSimulatorError

Bases: Exception

state vector simulation error

isqtools.backend.tensorflow_backend module

TensorFlow backend.

class TensorFlowBackend(dtype=None)

Bases: StateVectorSimulator

This method inherits the StateVectorSimulator, and uses tensorflow to perform the mathematical calculation.

H()
Return type:

Any

RX(theta)
Parameters:

theta (Any)

Return type:

Any

RXY(theta, phi)
Parameters:
  • theta (Any)

  • phi (Any)

Return type:

Any

RY(theta)
Parameters:

theta (Any)

Return type:

Any

RZ(theta)
Parameters:

theta (Any)

Return type:

Any

S()
Return type:

Any

SD()
Return type:

Any

T()
Return type:

Any

TD()
Return type:

Any

X()
Return type:

Any

X2M()
Return type:

Any

X2P()
Return type:

Any

Y()
Return type:

Any

Y2M()
Return type:

Any

Y2P()
Return type:

Any

Z()
Return type:

Any

as_tensor(tensor)
Parameters:

tensor (Any)

Return type:

Any

conj(state)
Parameters:

state (Any)

Return type:

Any

copy(state)
Parameters:

state (Any)

Return type:

Any

dot(state1, state2)
Parameters:
  • state1 (Any)

  • state2 (Any)

Return type:

Any

get_zero_state(qnum)
Parameters:

qnum (int)

Return type:

Any

ravel(state)
Parameters:

state (Any)

Return type:

Any

real(state)
Parameters:

state (Any)

Return type:

Any

reshape(state, shape)
Parameters:
  • state (Any)

  • shape (Sequence)

Return type:

Any

sqrt(state)
Parameters:

state (Any)

Return type:

Any

stack(states, axis=0)
Parameters:
  • states (Sequence[Any])

  • axis (int)

Return type:

Any

sum(state, axis=None)
Parameters:
  • state (Any)

  • axis (int | None)

Return type:

Any

isqtools.backend.tiany_backend module

class TianyBackend(token=None, machine_name='tianyan_sw', lab_id=None, max_wait_time=5, **kw)

Bases: HardwareBackend

The backend of Tianyan quantum hardware.

token (str, optional): API token for authentication just passed when you are the first time use this backend or want to change your token. Defaults to None.

machine_name (str, optional): Name of the machine. Defaults to full amplitude simulator .

max_wait_time (int, optional): Maximum wait time in seconds. Defaults to 24*3600 (24 hours).

lab_id (int | None, optional): Lab ID. Defaults to None.

Parameters:
  • token (str | None)

  • machine_name (str)

  • lab_id (int | None)

  • max_wait_time (int)

static get_homedir()
get_task_id(qcis, shots)
property query_id_single
query_sample_result(query_id_single, max_wait_time=1)
Parameters:
  • query_id_single (list[str])

  • max_wait_time (int)

sample(qcis, shots=100, **kwargs)

Call quantum hardware for sampling.

Parameters:
  • qcis (str)

  • shots (int)

Return type:

dict[str, int]

exception TianyBackendError

Bases: Exception

tianyan backend error

isqtools.backend.torch_backend module

PyTorch backend. PyTorch is a machine learning framework based on the Torch library, used for applications such as computer vision and natural language processing, originally developed by Meta AI and now part of the Linux Foundation umbrella. It is free and open-source software released under the modified BSD license. For more information, check out: https://pytorch.org/

class TorchBackend(dtype=None)

Bases: StateVectorSimulator

This method inherits the StateVectorSimulator, and uses pytorch to perform the mathematical calculation.

H()
Return type:

Any

RX(theta)
Parameters:

theta (Any)

Return type:

Any

RXY(theta, phi)
Parameters:
  • theta (Any)

  • phi (Any)

Return type:

Any

RY(theta)
Parameters:

theta (Any)

Return type:

Any

RZ(theta)
Parameters:

theta (Any)

Return type:

Any

S()
Return type:

Any

SD()
Return type:

Any

T()
Return type:

Any

TD()
Return type:

Any

X()
Return type:

Any

X2M()
Return type:

Any

X2P()
Return type:

Any

Y()
Return type:

Any

Y2M()
Return type:

Any

Y2P()
Return type:

Any

Z()
Return type:

Any

as_tensor(tensor)
Parameters:

tensor (Any)

Return type:

Any

conj(state)
Parameters:

state (Any)

Return type:

Any

copy(state)
Parameters:

state (Any)

Return type:

Any

dot(state1, state2)
Parameters:
  • state1 (Any)

  • state2 (Any)

Return type:

Any

get_zero_state(qnum)
Parameters:

qnum (int)

Return type:

Any

ravel(state)
Parameters:

state (Any)

Return type:

Any

real(state)
Parameters:

state (Any)

Return type:

Any

reshape(state, shape)
Parameters:
  • state (Any)

  • shape (Sequence)

Return type:

Any

sqrt(state)
Parameters:

state (Any)

Return type:

Any

stack(states, axis=0)
Parameters:
  • states (Sequence[Any])

  • axis (int)

Return type:

Any

sum(state, axis=None)
Parameters:
  • state (Any)

  • axis (int | None)

Return type:

Any

Module contents

All the backends needed for quantum circuit simulation. The quantum circuit is based on parsing the qcis file. The backend can be a simulator or hardware. The simulator is based on state vectors.