qns.models.qubit package

Submodules

qns.models.qubit.const module

qns.models.qubit.const.OPERATOR_PHASE_SHIFT(theta: float)[source]
qns.models.qubit.const.OPERATOR_RX(theta: float)[source]
qns.models.qubit.const.OPERATOR_RY(theta: float)[source]
qns.models.qubit.const.OPERATOR_RZ(theta: float)[source]

qns.models.qubit.decoherence module

qns.models.qubit.decoherence.DephaseMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for measuring this qubit, A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate)

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.decoherence.DephaseOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for operating this qubit. A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate)

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.decoherence.DephaseStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for storing a qubit in quantum memory.

A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate * t)

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.DephaseTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for transmitting a qubit in quantum channel.

A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate * length)

Parameters
  • length – the channel length

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.DepolarMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for measuring on a qubit.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.DepolarOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for operating on a qubit.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.DepolarStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for storing a qubit in quantum memory.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.DepolarTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for transmitting a qubit in quantum channel.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • length – the channel length

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.decoherence.PrefectMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for measuring this qubit.

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.decoherence.PrefectOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for operating this qubit.

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.decoherence.PrefectStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for storing a qubit in quantum memory. The default behavior is doing nothing

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate.

  • kwargs – other parameters

qns.models.qubit.decoherence.PrefectTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for transmitting this qubit The default behavior is doing nothing

Parameters
  • length (float) – the length of the channel

  • decoherence_rate (float) – the decoherence rate.

  • kwargs – other parameters

qns.models.qubit.errors module

exception qns.models.qubit.errors.OperatorError[source]

Bases: Exception

exception qns.models.qubit.errors.OperatorNotMatchError[source]

Bases: Exception

This error happens when the size of state vector or matrix mismatch occurs

exception qns.models.qubit.errors.QGateOperatorNotMatchError[source]

Bases: Exception

exception qns.models.qubit.errors.QGateQubitNotInStateError[source]

Bases: Exception

exception qns.models.qubit.errors.QGateStateJointError[source]

Bases: Exception

exception qns.models.qubit.errors.QStateBaseError[source]

Bases: Exception

exception qns.models.qubit.errors.QStateQubitNotInStateError[source]

Bases: Exception

exception qns.models.qubit.errors.QStateSizeNotMatchError[source]

Bases: Exception

This error happens when the size of state vector or matrix mismatch occurs

qns.models.qubit.factory module

class qns.models.qubit.factory.QubitFactory(operate_decoherence_rate: float = 0, measure_decoherence_rate: float = 0, store_error_model=<function PrefectStorageErrorModel>, transfer_error_model=<function PrefectTransferErrorModel>, operate_error_model=<function PrefectOperateErrorModel>, measure_error_model=<function PrefectMeasureErrorModel>)[source]

Bases: object

QubitFactory is the factory class for building qubits with special error models.

qns.models.qubit.gate module

class qns.models.qubit.gate.DoubleQubitsControlledGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = array([[0. + 0.j, 1. + 0.j], [1. + 0.j, 0. + 0.j]]), _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.Gate

The double qubits gates operate on two qubits, including a controlled qubit and a operating qubit.

The controlled gate:

[[I_2, 0][0, operator]]

class qns.models.qubit.gate.DoubleQubitsRotateGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = array([[0. + 0.j, 1. + 0.j], [1. + 0.j, 0. + 0.j]]), _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.DoubleQubitsControlledGate

class qns.models.qubit.gate.Gate(name: Optional[str] = None, _docs: Optional[str] = None)[source]

Bases: object

The quantum gates that will operate qubits

class qns.models.qubit.gate.SingleQubitArbitraryGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = None, _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.SingleQubitGate

class qns.models.qubit.gate.SingleQubitGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = None, _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.Gate

The single qubit gates operate on a single qubit

class qns.models.qubit.gate.SingleQubitRotateGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = None, _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.SingleQubitGate

class qns.models.qubit.gate.SwapGate(name: Optional[str] = None, _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.Gate

class qns.models.qubit.gate.ThreeQubitsGate(name: Optional[str] = None, operator: Optional[numpy.ndarray] = array([[0. + 0.j, 1. + 0.j], [1. + 0.j, 0. + 0.j]]), _docs: Optional[str] = None)[source]

Bases: qns.models.qubit.gate.Gate

The gate operates on three qubits, including 2 controlled qubit and a operating qubit.

The 3 controlled-controlled gate:

[[I_6, 0][0, operator]]

qns.models.qubit.qubit module

class qns.models.qubit.qubit.QState(qubits: List[qns.models.qubit.qubit.Qubit] = [], state: Optional[numpy.ndarray] = array([[1. + 0.j], [0. + 0.j]]), rho: Optional[numpy.ndarray] = None, name: Optional[str] = None)[source]

Bases: object

QState is the state of one (or multiple) qubits

equal(other_state: qns.models.qubit.qubit.QState) bool[source]

compare two state vectors, return True if they are the same

Parameters

other_state (QState) – the second QState

is_pure_state(eps: float = 1e-06) bool[source]
Parameters

eps – the accuracy

Returns

bool, if the state is a pure state

measure(qubit: Optional[qns.models.qubit.qubit.Qubit] = None, base: str = 'Z') int[source]

Measure this qubit using Z basis :param qubit: the measuring qubit :type qubit: Qubit :param base: the measure base, “Z”, “X” or “Y”

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

operate(operator: numpy.ndarray)[source]

transform using operator

Parameters

operator (np.ndarray) – the operator

Raises

OperatorNotMatchError

state() numpy.ndarray[source]

If the state is a pure state, return the state vector, or return None

Returns

The pure state vector

stochastic_operate(list_operators: List[numpy.ndarray] = [], list_p: List[float] = [])[source]

A stochastic operate progess. It usually turns a pure state into a mixed state.

Parameters
  • list_operators (List[np.ndarray]) – a list of operators

  • list_p (List[float]) – a list of possibility

Raises

OperatorNotMatchError

class qns.models.qubit.qubit.Qubit(state=array([[1. + 0.j], [0. + 0.j]]), rho: Optional[numpy.ndarray] = None, operate_decoherence_rate: float = 0, measure_decoherence_rate: float = 0, name: Optional[str] = None)[source]

Bases: qns.models.core.backend.QuantumModel

Represent a qubit

measure()[source]

Measure this qubit using Z basis

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

measureX()[source]

Measure this qubit using X basis.

Returns

QUBIT_STATE_P state 1: QUBIT_STATE_N state

Return type

0

measureY()[source]

Measure this qubit using Y basis. Only for not entangled qubits.

Returns

QUBIT_STATE_R state 1: QUBIT_STATE_L state

Return type

0

measureZ()[source]

Measure this qubit using Z basis

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

measure_error_model(decoherence_rate: Optional[float] = 0, **kwargs)[source]

The error model for measuring a qubit. This function will change the quantum state.

Parameters
  • decoherence_rate (float) – the decoherency rate

  • kwargs – other parameters

operate(operator: Any) None[source]

Perfrom a operate on this qubit

Parameters

operator (Union[SingleQubitGate, np.ndarray]) – an operator matrix, or a quantum gate in qubit.gate

operate_error_model(decoherence_rate: Optional[float] = 0, **kwargs)[source]

The error model for operating a qubit. This function will change the quantum state.

Parameters
  • decoherence_rate (float) – the decoherency rate

  • kwargs – other parameters

stochastic_operate(list_operators: List[numpy.ndarray] = [], list_p: List[float] = [])[source]

A stochastic operate on this qubit. It usually turns a pure state into a mixed state.

Parameters
  • list_operators (List[np.ndarray]) – a list of operators

  • list_p (List[float]) – a list of possibility

Raises

OperatorNotMatchError

store_error_model(t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for storing a qubit in quantum memory. The default behavior is doing nothing

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate in Db.

  • kwargs – other parameters

transfer_error_model(length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for transmitting this qubit The default behavior is doing nothing

Parameters
  • length (float) – the length of the channel

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.utils module

qns.models.qubit.utils.joint(qubit1, qubit2) None[source]
qns.models.qubit.utils.partial_trace(rho: numpy.ndarray, idx: int) numpy.ndarray[source]

Calculate the partial trace

Parameters
  • rho – the density matrix

  • idx (int) – the index of removing qubit

Returns

the left density matric

Return type

rho_res

qns.models.qubit.utils.single_gate_expand(qubit, operator: numpy.ndarray) numpy.ndarray[source]

Module contents

qns.models.qubit.DephaseMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for measuring this qubit, A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate)

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.DephaseOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for operating this qubit. A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate)

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.DephaseStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for storing a qubit in quantum memory.

A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate * t)

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.DephaseTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The dephase error model for transmitting a qubit in quantum channel.

A random Z gate will be operate on the qubit with possibility: 1-e^(-decoherence_rate * length)

Parameters
  • length – the channel length

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.DepolarMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for measuring on a qubit.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.DepolarOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for operating on a qubit.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.DepolarStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for storing a qubit in quantum memory.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.DepolarTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The depolar error model for transmitting a qubit in quantum channel.

One of the random Pauli gate will be operate on the qubit with possibility:

1-e^(-decoherence_rate * t)

Parameters
  • length – the channel length

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

qns.models.qubit.PrefectMeasureErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for measuring this qubit.

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.PrefectOperateErrorModel(self, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for operating this qubit.

Parameters

decoherence_rate (float) – the decoherence rate

qns.models.qubit.PrefectStorageErrorModel(self, t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for storing a qubit in quantum memory. The default behavior is doing nothing

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate.

  • kwargs – other parameters

qns.models.qubit.PrefectTransferErrorModel(self, length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for transmitting this qubit The default behavior is doing nothing

Parameters
  • length (float) – the length of the channel

  • decoherence_rate (float) – the decoherence rate.

  • kwargs – other parameters

class qns.models.qubit.QState(qubits: List[qns.models.qubit.qubit.Qubit] = [], state: Optional[numpy.ndarray] = array([[1. + 0.j], [0. + 0.j]]), rho: Optional[numpy.ndarray] = None, name: Optional[str] = None)[source]

Bases: object

QState is the state of one (or multiple) qubits

equal(other_state: qns.models.qubit.qubit.QState) bool[source]

compare two state vectors, return True if they are the same

Parameters

other_state (QState) – the second QState

is_pure_state(eps: float = 1e-06) bool[source]
Parameters

eps – the accuracy

Returns

bool, if the state is a pure state

measure(qubit: Optional[qns.models.qubit.qubit.Qubit] = None, base: str = 'Z') int[source]

Measure this qubit using Z basis :param qubit: the measuring qubit :type qubit: Qubit :param base: the measure base, “Z”, “X” or “Y”

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

operate(operator: numpy.ndarray)[source]

transform using operator

Parameters

operator (np.ndarray) – the operator

Raises

OperatorNotMatchError

state() numpy.ndarray[source]

If the state is a pure state, return the state vector, or return None

Returns

The pure state vector

stochastic_operate(list_operators: List[numpy.ndarray] = [], list_p: List[float] = [])[source]

A stochastic operate progess. It usually turns a pure state into a mixed state.

Parameters
  • list_operators (List[np.ndarray]) – a list of operators

  • list_p (List[float]) – a list of possibility

Raises

OperatorNotMatchError

class qns.models.qubit.Qubit(state=array([[1. + 0.j], [0. + 0.j]]), rho: Optional[numpy.ndarray] = None, operate_decoherence_rate: float = 0, measure_decoherence_rate: float = 0, name: Optional[str] = None)[source]

Bases: qns.models.core.backend.QuantumModel

Represent a qubit

measure()[source]

Measure this qubit using Z basis

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

measureX()[source]

Measure this qubit using X basis.

Returns

QUBIT_STATE_P state 1: QUBIT_STATE_N state

Return type

0

measureY()[source]

Measure this qubit using Y basis. Only for not entangled qubits.

Returns

QUBIT_STATE_R state 1: QUBIT_STATE_L state

Return type

0

measureZ()[source]

Measure this qubit using Z basis

Returns

QUBIT_STATE_0 state 1: QUBIT_STATE_1 state

Return type

0

measure_error_model(decoherence_rate: Optional[float] = 0, **kwargs)[source]

The error model for measuring a qubit. This function will change the quantum state.

Parameters
  • decoherence_rate (float) – the decoherency rate

  • kwargs – other parameters

operate(operator: Any) None[source]

Perfrom a operate on this qubit

Parameters

operator (Union[SingleQubitGate, np.ndarray]) – an operator matrix, or a quantum gate in qubit.gate

operate_error_model(decoherence_rate: Optional[float] = 0, **kwargs)[source]

The error model for operating a qubit. This function will change the quantum state.

Parameters
  • decoherence_rate (float) – the decoherency rate

  • kwargs – other parameters

stochastic_operate(list_operators: List[numpy.ndarray] = [], list_p: List[float] = [])[source]

A stochastic operate on this qubit. It usually turns a pure state into a mixed state.

Parameters
  • list_operators (List[np.ndarray]) – a list of operators

  • list_p (List[float]) – a list of possibility

Raises

OperatorNotMatchError

store_error_model(t: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for storing a qubit in quantum memory. The default behavior is doing nothing

Parameters
  • t – the time stored in a quantum memory. The unit it second.

  • decoherence_rate (float) – the decoherence rate in Db.

  • kwargs – other parameters

transfer_error_model(length: Optional[float] = 0, decoherence_rate: Optional[float] = 0, **kwargs)[source]

The default error model for transmitting this qubit The default behavior is doing nothing

Parameters
  • length (float) – the length of the channel

  • decoherence_rate (float) – the decoherence rate

  • kwargs – other parameters

class qns.models.qubit.QubitFactory(operate_decoherence_rate: float = 0, measure_decoherence_rate: float = 0, store_error_model=<function PrefectStorageErrorModel>, transfer_error_model=<function PrefectTransferErrorModel>, operate_error_model=<function PrefectOperateErrorModel>, measure_error_model=<function PrefectMeasureErrorModel>)[source]

Bases: object

QubitFactory is the factory class for building qubits with special error models.

qns.models.qubit.joint(qubit1, qubit2) None[source]
qns.models.qubit.single_gate_expand(qubit, operator: numpy.ndarray) numpy.ndarray[source]