跳转至

Qubit

QuICT.core.qubit.Qubit

Qubit(fidelity: Union[float, tuple] = 1.0, preparation_fidelity: float = 1.0, gate_fidelity: Union[float, dict] = 1.0, T1: float = 0.0, T2: float = 0.0, work_frequency: float = 0.0, readout_frequency: float = 0.0, gate_duration: float = 0.0)

Bases: object

Implement a Quantum bit

Qubit is the basic unit of Quantum Compute.

Parameters:

  • fidelity (Union[float, tuple], default: 1.0 ) –

    The qubit's measured fidelity, where the fidelity of a quantum qubit is the overlap between the ideal theoretical operation and the actual experimental operation. if it is list, it represent the measured fidelity for state 0 and state 1.

  • preparation_fidelity (float, default: 1.0 ) –

    The qubit's state preparation fidelity refers to the degree of accuracy with which a quantum bit (qubit) can be prepared in a specific state.

  • gate_fidelity (Union[float, dict], default: 1.0 ) –

    The fidelity for applying single-qubit quantum gate in this qubit. e.g. {GateType.h: 0.993, GateType.x: 0.989}

  • T1 ((float, μs), default: 0.0 ) –

    The longitudinal coherence time, which refers to the time it takes for the qubit to decay back to its ground state from an excited state. Default to None.

  • T2 ((float, μs), default: 0.0 ) –

    the transverse coherence time, which refers to the time it takes for the qubit to lose its coherence when subjected to unwanted phase or amplitude fluctuations. Default to None.

  • work_frequency (Union[float, list], default: 0.0 ) –

    The working frequency in current Qubit.

  • readout_frequency (Union[float, list], default: 0.0 ) –

    The frequency when measured qubit in current Qubit.

  • gate_duration (Union[float, list], default: 0.0 ) –

    The amount of time that a Quantum Gate operators on a Qubit.

Source code in QuICT/core/qubit/qubit.py
def __init__(
    self,
    fidelity: Union[float, tuple] = 1.0,
    preparation_fidelity: float = 1.0,
    gate_fidelity: Union[float, dict] = 1.0,
    T1: float = 0.0,
    T2: float = 0.0,
    work_frequency: float = 0.0,
    readout_frequency: float = 0.0,
    gate_duration: float = 0.0,
):
    """
    Args:
        fidelity (Union[float, tuple]): The qubit's measured fidelity, where the fidelity of a quantum qubit is the
            overlap between the ideal theoretical operation and the actual experimental operation. if it is list,
            it represent the measured fidelity for state 0 and state 1.
        preparation_fidelity (float): The qubit's state preparation fidelity refers to the degree of accuracy with
            which a quantum bit (qubit) can be prepared in a specific state.
        gate_fidelity (Union[float, dict]): The fidelity for applying single-qubit quantum gate in this qubit.
            e.g. {GateType.h: 0.993, GateType.x: 0.989}
        T1 (float, μs): The longitudinal coherence time, which refers to the time it takes for the qubit to decay
            back to its ground state from an excited state. Default to None.
        T2 (float, μs): the transverse coherence time, which refers to the time it takes for the qubit to lose its
            coherence when subjected to unwanted phase or amplitude fluctuations. Default to None.
        work_frequency (Union[float, list]): The working frequency in current Qubit.
        readout_frequency (Union[float, list]): The frequency when measured qubit in current Qubit.
        gate_duration (Union[float, list]): The amount of time that a Quantum Gate operators on a Qubit.
    """
    self._id = unique_id_generator()
    self.fidelity = fidelity
    self.preparation_fidelity = preparation_fidelity
    self.gate_fidelity = gate_fidelity
    self.T1 = T1
    self.T2 = T2
    self.work_frequency = work_frequency
    self.readout_frequency = readout_frequency
    self.gate_duration = gate_duration

    self._measured = None
    self._probability = None    # The prob of measured 0
    self._historical_measured = []

__bool__

__bool__()

int value of the qubit(measure result)

Returns:

  • bool

    measure result

Source code in QuICT/core/qubit/qubit.py
def __bool__(self):
    """ int value of the qubit(measure result)

    Returns:
        bool: measure result

    Raises:
        The qubit has not be measured.
    """
    return bool(int(self))

__int__

__int__()

int value of the qubit(measure result)

Returns:

  • int

    measure result

Source code in QuICT/core/qubit/qubit.py
def __int__(self):
    """ int value of the qubit(measure result)

    Returns:
        int: measure result

    Raises:
        The qubit has not be measured.
    """
    if self._measured is None:
        raise QubitMeasureError(f"The qubit {self.id} has not be measured")

    return self.measured

__str__

__str__()

string describe of the qubit

Returns:

  • str

    a simple describe

Source code in QuICT/core/qubit/qubit.py
def __str__(self):
    """ string describe of the qubit

    Returns:
        str: a simple describe
    """
    return f"qubit id: {self.id}; fidelity: {self.fidelity}; QSP_fidelity: {self.preparation_fidelity}; " \
        + f"Gate_fidelity: {self.gate_fidelity}; Coherence time: T1: {self._t1}; T2: {self._t2}; " \
        + f"Work Frequency: {self.work_frequency}; Readout Frequency: {self.readout_frequency}; " \
        + f"Gate Duration: {self.gate_duration}"

reset

reset()

Reset self qubit status.

Source code in QuICT/core/qubit/qubit.py
def reset(self):
    """ Reset self qubit status. """
    self._measured = None
    self._historical_measured = []

QuICT.core.qubit.Qureg

Qureg(qubits: Union[int, Qubit, Qureg] = None)

Bases: list

Implement a Quantum Register

Qureg is a list of Qubits, which is a subClass of list.

initial a qureg with qubit(s)

Parameters:

  • qubits (Union[int, Qubit, Qureg], default: None ) –

    the qubits which make up the qureg, it can have below form, 1) int 2) qubit 3) [qubits/quregs]

Source code in QuICT/core/qubit/qubit.py
def __init__(self, qubits: Union[int, Qubit, Qureg] = None):
    """ initial a qureg with qubit(s)

    Args:
        qubits: the qubits which make up the qureg, it can have below form,
            1) int
            2) qubit
            3) [qubits/quregs]
    """
    super().__init__()
    if qubits is None:
        return

    if isinstance(qubits, int):
        for _ in range(qubits):
            self.append(Qubit())
    elif isinstance(qubits, Qubit):
        self.append(qubits)
    elif isinstance(qubits, list):
        for qubit in qubits:
            if isinstance(qubit, Qubit):
                self.append(qubit)
            elif isinstance(qubit, Qureg):
                self.extend(qubit)
            else:
                raise TypeError("Qureg.qubits", "int/Qubit/list<Qubit/Qureg>", type(qubit))
    else:
        raise TypeError("Qureg.qubits", "int/Qubit/list<Qubit/Qureg>", type(qubits))

__add__

__add__(other)

get a combined qureg with this qureg and other qureg

Parameters:

  • other(Qureg)

    qureg to be added.

Return

Qureg: the result or slice

Source code in QuICT/core/qubit/qubit.py
def __add__(self, other):
    """ get a combined qureg with this qureg and other qureg

    Args:
        other(Qureg): qureg to be added.

    Return:
        Qureg: the result or slice
    """
    return Qureg([self, other])

__call__

__call__(indexes: object)

get a smaller qureg from this qureg

Parameters:

  • indexes (object) –

    the indexes passed in, it can have follow form: 1) int 2) list

Returns: Qubit[s]: the qureg correspond to the indexes

Source code in QuICT/core/qubit/qubit.py
def __call__(self, indexes: object):
    """ get a smaller qureg from this qureg

    Args:
        indexes: the indexes passed in, it can have follow form:
            1) int
            2) list<int>
    Returns:
        Qubit[s]: the qureg correspond to the indexes
    """
    if isinstance(indexes, int):
        return Qureg(self[indexes])

    return self[indexes]

__eq__

__eq__(other)

check two qureg is same or not. Iff all qubits in two qureg are same will return True; otherwise, return False.

Parameters:

  • other(Qureg)

    qureg to be checked.

Source code in QuICT/core/qubit/qubit.py
def __eq__(self, other):
    """
    check two qureg is same or not. Iff all qubits in two qureg are same will
    return True; otherwise, return False.

    Args:
        other(Qureg): qureg to be checked.
    """
    assert isinstance(other, Qureg), TypeError("Qureg.eq", "Qureg", type(other))
    if not len(other) == len(self):
        return False

    current_qubit_ids = [qubit.id for qubit in self]
    for qubit in other:
        if qubit.id not in current_qubit_ids:
            return False

    return True

__getitem__

__getitem__(item)

to fit the slice operator, overloaded this function.

get a smaller qureg/qubit from this qureg

Parameters:

  • item(int/slice)

    slice passed in.

Return

Qubit/Qureg: the result or slice

Source code in QuICT/core/qubit/qubit.py
def __getitem__(self, item):
    """ to fit the slice operator, overloaded this function.

    get a smaller qureg/qubit from this qureg

    Args:
        item(int/slice): slice passed in.

    Return:
        Qubit/Qureg: the result or slice
    """
    if isinstance(item, int):
        return super().__getitem__(item)

    qureg = Qureg()
    if isinstance(item, slice):
        qureg_list = super().__getitem__(item)
        for qubit in qureg_list:
            qureg.append(qubit)
    elif isinstance(item, list) or isinstance(item, tuple):
        for idx in item:
            assert isinstance(idx, int), TypeError("Qureg.getitem.item.value", "int", type(idx))
            if idx < 0 or idx > len(self):
                raise IndexExceedError("Qureg.getitem", [0, len(self)], idx)

            qureg.append(self[idx])
    else:
        raise TypeError("Qureg.getitem", "int/list[int]/slice", type(item))

    return qureg

__iadd__

__iadd__(other)

get a combined qureg with this qureg and other qureg

Parameters:

  • other(Qureg)

    qureg to be added.

Return

Qureg: the result or slice

Source code in QuICT/core/qubit/qubit.py
def __iadd__(self, other):
    """ get a combined qureg with this qureg and other qureg

    Args:
        other(Qureg): qureg to be added.

    Return:
        Qureg: the result or slice
    """
    if isinstance(other, Qubit):
        self.append(other)
    elif isinstance(other, Qureg):
        self.extend(other)
    else:
        raise TypeError("Qureg.iadd", "Qureg/Qubit", type(other))

    return self

__int__

__int__()

the value of the register

Return the value of the register if all qubits have been measured. Note that the compute mode is BigEndian.

Returns:

  • int

    the value of the register

Raises:

  • Exception

    some qubit has not be measured

Source code in QuICT/core/qubit/qubit.py
def __int__(self):
    """ the value of the register

    Return the value of the register if all qubits have been measured.
    Note that the compute mode is BigEndian.

    Returns:
        int: the value of the register

    Raises:
        Exception: some qubit has not be measured
    """
    value = 0
    for qubit in self:
        value <<= 1
        value += int(qubit)

    return value

__str__

__str__()

the string of the value of the register

Returns:

  • str

    the value of the qureg

Source code in QuICT/core/qubit/qubit.py
def __str__(self):
    """ the string of the value of the register

    Returns:
        str: the value of the qureg
    """
    bit_idx = "{0:0b}".format(self.__int__())
    bit_idx = bit_idx.zfill(len(self))

    return bit_idx

index

index(qubit: Union[List[Qubit], Qubit]) -> Union[int, list]

Return the index of given qubits.

Parameters:

  • qubit (Union[List[Qubit], Qubit]) –

    The given qubits

Returns:

  • Union[int, list]

    Union[int, list]: The index of given qubits in current qureg.

Source code in QuICT/core/qubit/qubit.py
def index(self, qubit: Union[List[Qubit], Qubit]) -> Union[int, list]:
    """ Return the index of given qubits.

    Args:
        qubit (Union[List[Qubit], Qubit]): The given qubits

    Returns:
        Union[int, list]: The index of given qubits in current qureg.
    """
    if isinstance(qubit, Qubit):
        return super().index(qubit)

    if isinstance(qubit, list):
        idxes = []
        for q in qubit:
            idxes.append(super().index(q))

        return idxes

    raise ValueError("Qureg.index.qubit", "within current Qureg", "qubit is not")

reset_qubits

reset_qubits()

Reset all qubits' status.

Source code in QuICT/core/qubit/qubit.py
def reset_qubits(self):
    """ Reset all qubits' status. """
    for qubit in self:
        qubit.reset()

set_fidelity

set_fidelity(fidelity: Union[float, tuple, list])

Set the fidelity for each qubits

Parameters:

  • fidelity (list) –

    The list of fidelity for each qubits, should equal to len(qureg).

Source code in QuICT/core/qubit/qubit.py
def set_fidelity(self, fidelity: Union[float, tuple, list]):
    """ Set the fidelity for each qubits

    Args:
        fidelity (list): The list of fidelity for each qubits, should equal to len(qureg).
    """
    fidelity = self._normalized_parameters(fidelity, "fidelity")
    for idx, qubit in enumerate(self):
        qubit.fidelity = fidelity[idx]

set_gate_fidelity

set_gate_fidelity(gate_fidelity: Union[float, list])

Set the Single-Qubit Gate Fidelity for each qubits

Parameters:

  • gate_fidelity (list) –

    The list of gate fidelity for each qubits, should equal to len(qureg).

Source code in QuICT/core/qubit/qubit.py
def set_gate_fidelity(self, gate_fidelity: Union[float, list]):
    """ Set the Single-Qubit Gate Fidelity for each qubits

    Args:
        gate_fidelity (list): The list of gate fidelity for each qubits, should equal to len(qureg).
    """
    gate_fidelity = self._normalized_parameters(gate_fidelity, "gate_fidelity")
    for idx, qubit in enumerate(self):
        qubit.gate_fidelity = gate_fidelity[idx]

set_preparation_fidelity

set_preparation_fidelity(fidelity: Union[float, list])

Set the QSP fidelity for each qubits

Parameters:

  • fidelity (list) –

    The list of fidelity for each qubits, should equal to len(qureg).

Source code in QuICT/core/qubit/qubit.py
def set_preparation_fidelity(self, fidelity: Union[float, list]):
    """ Set the QSP fidelity for each qubits

    Args:
        fidelity (list): The list of fidelity for each qubits, should equal to len(qureg).
    """
    fidelity = self._normalized_parameters(fidelity, "preparation_fidelity")
    for idx, qubit in enumerate(self):
        qubit.preparation_fidelity = fidelity[idx]

set_t1_time

set_t1_time(t1_time: list)

Set the T1 coherence time for each qubit

Parameters:

  • t1_time (list) –

    The T1 time for each qubit

Source code in QuICT/core/qubit/qubit.py
def set_t1_time(self, t1_time: list):
    """ Set the T1 coherence time for each qubit

    Args:
        t1_time (list): The T1 time for each qubit
    """
    t1_time = self._normalized_parameters(t1_time, "t1")
    for idx, qubit in enumerate(self):
        qubit.T1 = t1_time[idx]

set_t2_time

set_t2_time(t2_time: list)

Set the T2 coherence time for each qubit

Parameters:

  • t2_time (list) –

    The T2 time for each qubit

Source code in QuICT/core/qubit/qubit.py
def set_t2_time(self, t2_time: list):
    """ Set the T2 coherence time for each qubit

    Args:
        t2_time (list): The T2 time for each qubit
    """
    t2_time = self._normalized_parameters(t2_time, "t2")
    for idx, qubit in enumerate(self):
        qubit.T2 = t2_time[idx]