跳转至

CircuitLib

QuICT.tools.circuit_library.CircuitLib

CircuitLib(output_type: str = 'circuit', output_path: str = '.')

A class handling QuICT circuit library.

Parameters:

  • output_type (str, default: 'circuit' ) –

    one of [circuit, qasm, file]. Defaults to "circuit".

  • output_path (str, default: '.' ) –

    The path to store qasm file if output type is file. Defaults to the current working path.

Source code in QuICT/tools/circuit_library/circuitlib.py
def __init__(
    self,
    output_type: str = "circuit",
    output_path: str = '.'
):
    self._output_type = output_type
    self._output_path = output_path
    logger.debug(f"Initial Circuit Library with output {self._output_type}.")

    self._db = CircuitLibDB()
    if output_type == "file":
        if not os.path.isdir(output_path):
            os.makedirs(output_path)

get_circuit

get_circuit(type: str, classify: str = None, qubits_interval: Union[list, int] = None) -> Union[List[Union[Circuit, str]], None]

Get the target circuits from QuICT Circuit Library.

Parameters:

  • type (str) –

    The type of circuits, one of [machine, algorithm].

  • classify (str, default: None ) –

    The classify of selected circuit's type. For machine circuit's type, classify is one of [aspen-4, ourense, rochester, sycamore, tokyo] For algorithm circuit's type, classify is one of [adder, clifford, qnn, grover, qft, vqe, cnf, maxcut, quantum_walk]

  • qubits_interval (Union[List, int], default: None ) –

    The interval of qubit number, if it givens an interger, it equals to the interval of [1, qubits_interval], Otherwise, if it is list, it should contains the minimal qubits number and maximul qubits number.

Returns:

  • List[Circuit | String] | None

    Return the list of output circuit order by output_type.

Source code in QuICT/tools/circuit_library/circuitlib.py
def get_circuit(
    self,
    type: str,
    classify: str = None,
    qubits_interval: Union[list, int] = None
) -> Union[List[Union[Circuit, str]], None]:
    """Get the target circuits from QuICT Circuit Library.

    Args:
        type (str): The type of circuits, one of [machine, algorithm].
        classify (str, optional): The classify of selected circuit's type.
            For machine circuit's type, classify is one of
                [aspen-4, ourense, rochester, sycamore, tokyo]
            For algorithm circuit's type, classify is one of
                [adder, clifford, qnn, grover, qft, vqe, cnf, maxcut, quantum_walk]

        qubits_interval (Union[List, int], optional): The interval of qubit number, if it givens an interger,
            it equals to the interval of [1, qubits_interval], Otherwise, if it is list, it should contains the
            minimal qubits number and maximul qubits number.

    Returns:
        (List[Circuit | String] | None): Return the list of output circuit order by output_type.
    """
    if type not in self.__DEFAULT_TYPE:
        raise KeyError("error_type")

    if classify is not None:
        assert classify in self.__DEFAULT_CLASSIFY[type], "error classify."
        classify_list = [classify]
    else:
        classify_list = self.__DEFAULT_CLASSIFY[type]

    if isinstance(qubits_interval, list):
        assert len(qubits_interval) == 2, f"{len(qubits_interval)}"

    circuit_list = []
    for classify in classify_list:
        path = os.path.join(self.__LIB_PATH, type, classify)
        files = self._db.circuit_filter(type, classify, qubits_interval)
        circuit_list += self._get_all(path, files)

    return circuit_list

get_template_circuit

get_template_circuit(qubits_interval: Union[list, int] = None, max_size: int = None, max_depth: int = None, typelist: list = None) -> Union[List[Union[Circuit, str]], None]

Get template circuits in QuICT circuit library. A template will be loaded if it satisfies the following restrictions: 1. the circuit in the given classify. 2. its number of qubits <= max_width, 3. its number of gates <= max_size, 4. its depth <= max_depth. 5. its gates' types are in typelist

Restrictions will be ignored if not specified.

Parameters:

  • qubits_interval (Union[List, int], default: None ) –

    The interval of qubit number, if it givens an interger, it equals to the interval of [1, qubits_interval], Otherwise, if it is list, it should contains the minimal qubits number and maximul qubits number. The range should be [1, 5].

  • max_size(int)

    max number of gates, range is [2, 6].

  • max_depth(int)

    max depth of circuit, range is [2, 9].

  • typelist(Iterable[GateType])

    list of allowed gate types

Returns:

  • List[Circuit | String] | None

    Return the list of output circuit order by output_type.

Source code in QuICT/tools/circuit_library/circuitlib.py
def get_template_circuit(
    self,
    qubits_interval: Union[list, int] = None,
    max_size: int = None,
    max_depth: int = None,
    typelist: list = None
) -> Union[List[Union[Circuit, str]], None]:
    """
    Get template circuits in QuICT circuit library. A template will be loaded if
    it satisfies the following restrictions:
        1. the circuit in the given classify.
        2. its number of qubits <= `max_width`,
        3. its number of gates <= `max_size`,
        4. its depth <= `max_depth`.
        5. its gates' types are in `typelist`

    Restrictions will be ignored if not specified.

    Args:
        qubits_interval (Union[List, int], optional): The interval of qubit number, if it givens an interger,
            it equals to the interval of [1, qubits_interval], Otherwise, if it is list, it should contains the
            minimal qubits number and maximul qubits number. The range should be [1, 5].
        max_size(int): max number of gates, range is [2, 6].
        max_depth(int): max depth of circuit, range is [2, 9].
        typelist(Iterable[GateType]): list of allowed gate types

    Returns:
        (List[Circuit | String] | None): Return the list of output circuit order by output_type.
    """
    path = os.path.join(self.__LIB_PATH, "template")
    files = self._db.circuit_filter("template", "template", qubits_interval, max_size, max_depth)

    if typelist is not None:
        target_type = [gtype.name for gtype in typelist]
        valid_files = []
        for f in files:
            valid = True
            temp_f: str = f[0][:-5]
            g_info = temp_f.split('_')[4:]
            for gi in g_info:
                if gi not in target_type:
                    valid = False
                    continue

            if valid:
                valid_files.append(f)
    else:
        valid_files = files

    return self._get_all(path, valid_files)