cqlib.utils package

Utils module defines classes and functions to support quantum computing. It includes two submodules: - qasm_to_qcis: convert qasm order to qcis order. - qcis_to_qasm: convert qcis order to qasm order

Additionally, utils module contains utility functions for laboratory experiments and function to simplify qasm order and qcis order.

Subpackages

Submodules

cqlib.utils.const module

cqlib.utils.laboratory_utils module

class cqlib.utils.laboratory_utils.LaboratoryUtils(machine_name: str | None = None)

Bases: object

assign_parameters(circuits: List[str], parameters: List[List], values: List[List])

Assign parameter values to circuit template strings based on provided parameter lists.

Parameters:
  • circuits (List[str]) – A list of circuit template QCIS strings which may contain parameter placeholders.

  • parameters (List[List]) – A list of parameter names corresponding to the values list.

  • values (List[List]) – A list of parameter values corresponding to the parameters list.

Returns:

A list containing each circuit template string after parameter substitution.

Returns an empty string if parameter matching fails or the number of parameters mismatches.

Return type:

List[str]

convert_qasm_to_qcis(qasm: str, qubit_map: Dict | None = None)

Convert qasm string to qcis string

Parameters:
  • qasm – qasm string.

  • qubit_map

    Number mapping in qasm, where the value is None,

    directly maps bits based on the format of number plus 1. Defaults to None.

    Raises:

    Exception: language conversion failed.

    Returns:

    str: simplified qcis string.

convert_qasm_to_qcis_from_file(qasm_file: str, qubit_map: Dict | None = None)

Read qasm from file and convert it to qcis

Parameters:
  • qasm_file – qasm file.

  • qubit_map – Number mapping in qasm, where the value is None, directly maps bits based on the format of number plus 1. Defaults to None.

Raises:

Exception – language conversion failed.

Returns:

simplified qcis.

Return type:

str

convert_qcis_to_qasm(qcis: str)

convert qcis to qasm string.

Parameters:

qcis – qcis

Returns:

converted qasm.

Return type:

str

get_coupling_map(config_json)

Constructs an adjacency list representing the coupling map of qubits.

Parses the configuration file to extract the overview of qubits and coupler information, filters out unused qubits, and generates an adjacency list depicting the coupling relationship between qubits.

Parameters:

config_json (dict) – A dictionary containing the configuration details of the quantum processor, including qubit lists and coupling maps.

Returns:

A list representing the coupling relationships between qubits as an adjacency list.

Return type:

adjacency_list (list)

original_onversion_part(state01)

Calculates the probability distribution of measurement outcomes for a given quantum state.

Parameters:

state01 (list) – A one-dimensional or two-dimensional list representing the quantum state, with complex numbers as elements.

Returns:

A dictionary where keys are binary strings representing measurement outcomes and values are their corresponding probabilities.

Return type:

dict

original_onversion_whole(state01)

Calculates the probability distribution of measurement outcomes based on the given quantum state.

Parameters:

state01 – A one-dimensional or two-dimensional list representing the quantum state, with elements as complex numbers.

Returns:

A dictionary with keys as binary strings of measurement results and values as corresponding probabilities.

Return type:

Dict

probability_calibration(result: Dict, laboratory, config_json: Dict | None = None)

correction of the measured probability of 01 quantum state.

Parameters:
  • result – the results returned after query_experiment.

  • config_json – experimental parameters of quantum computer. config_json value is None, read the latest experimental parameters for calculation. Defaults to None.

  • laboratory

Raises:

Exception – cannot calibrate probability with fidelity.

Returns:

corrected probability.

Return type:

Dict

probability_correction(probabilities)
correction of the measured probability of 01 quantum state.

If there is a probability greater than 1, change this item to 1. If there is anything less than 0, change the item to 0.

Parameters:

probabilities – corrected probability.

Returns:

corrected probability.

Return type:

Dict

readout_data_to_state_probabilities(result)

Converts readout data into a list of state probabilities.

Parses the measurement results to derive the state of each qubit and represents it as a binary-formatted list. This function effectively translates the measurement outcomes into the probability of the quantum system being in a specific state post-measurement.

Parameters:

result (dict) – A dictionary containing the measurement results, where the ‘.resultStatus’ entry is a list with the first item being the number of qubits, followed by the measurement outcomes for each qubit.

Returns:

A list of each qubit’s states, with each qubit’s state represented

as a boolean list; True signifies the qubit is in state 1, and False indicates state 0.

Return type:

list

readout_data_to_state_probabilities_part(result: Dict)

read data and convert it into a quantum state probability, do not return with a probability of 0.

Parameters:

result – the results returned after query_experiment.

Returns:

probability

Return type:

Dict

readout_data_to_state_probabilities_whole(result: Dict)

read data and convert it into a quantum state probability, all returns.

Parameters:

result – the results returned after query_experiment.

Returns:

probability

Return type:

Dict

simplify_qcis(qcis_raw: str)

simplification of qcis lines. If simplification fails, prompt an error message and return the original qcis circuit.

Parameters:

qcis_raw – qcis

Returns:

simplified qcis.

Return type:

str

cqlib.utils.qcis_to_qasm module

class cqlib.utils.qcis_to_qasm.QcisToQasm

Bases: object

Class for converting quantum circuit instructions from qcis format to qasm format.

exception GateNotImplemented

Bases: Exception

Exception raised for gates that are not implemented in the conversion process.

exception InputParaNumError

Bases: Exception

Exception raised when the number of input parameters for a gate does not match the requirement.

static convert_qcis_to_qasm(qcis_input: str) str

Converts a string of qcis formatted quantum circuit instructions to qasm format.

Parameters:

qcis_input – A string containing qcis formatted quantum circuit instructions.

Returns:

A string of qasm formatted quantum circuit instructions.

convert_rule = {'B': 'barrier q[{0}],q[{1}];', 'CZ': 'cz q[{0}],q[{1}];', 'H': 'h q[{0}];', 'I': '', 'RX': 'rx({1}) q[{0}];', 'RXY': <function QcisToQasm._rxy_qcis_to_qasm>, 'RY': 'ry({1}) q[{0}];', 'RZ': 'rz({1}) q[{0}];', 'S': 's q[{0}];', 'SD': 'sdg q[{0}];', 'T': 't q[{0}];', 'TD': 'tdg q[{0}];', 'X': 'x q[{0}];', 'X2M': 'sxdg q[{0}];', 'X2P': 'sx q[{0}];', 'XY': <function QcisToQasm._xy_qcis_to_qasm>, 'XY2M': <function QcisToQasm._xy2m_qcis_to_qasm>, 'XY2P': <function QcisToQasm._xy2p_qcis_to_qasm>, 'Y': 'y q[{0}];', 'Y2M': 'ry(-1.5707963267948966) q[{0}];', 'Y2P': 'ry(1.5707963267948966) q[{0}];', 'Z': 'z q[{0}];'}

cqlib.utils.simplify module

class cqlib.utils.simplify.QCIS_Simplify

Bases: object

The class to simplify qcis code.

check_conversion()

Check whether rz_param_conversion needed.

Iterates through each rule in the check rule list. For each rule, it contains a set of conversion keywords. If any keyword from the rules is found in the instruction, it indicates that conversion is required.

Returns:

True if the instruction requires conversion; otherwise, False.

Return type:

bool

check_optimization_continue()

Check whether the optimization process should proceed.

Returns:

True if the optimization should proceed, False otherwise.

Return type:

bool

find_qubit_by_qasm(conversion_value)

Finds qubit indices from a QASM conversion value.

Parses the given conversion_value to extract qubit indices enclosed in square brackets. Returns a list of unique qubit indices.

Parameters:

conversion_value (str) – A string containing the QASM conversion value with qubit indices represented as “[index]”.

Returns:

A list of unique integers representing the qubit indices found in the conversion_value.

Return type:

list

repeat_rz()

Merge consecutive RZ gate operations.

This method iterates through quantum instruction string, replacing RZ parameters with their actual values. It then identifies and merges consecutive RZ gates to simplify the circuit and potentially reduce execution time.

rz_params

A dictionary containing parameters to be replaced in RZ instructions.

Type:

dict

qcis_instr

The quantum circuit instruction string where replacements and merges occur.

Type:

str

rz_period

The period used for modulo operations to normalize RZ parameters within the range [-π, π].

Type:

float

replace_conversion(rz_pi_flag=False)

Replace RZ gates with equivalent gates to simplify the circuit.

Parameters:

rz_pi_flag (bool) – Flag indicating whether to replace RZ(π) with RZ(-π).

simplify(qcis)

Simplifies the given quantum circuit instruction string.

This method attempts to simplify the quantum circuit instructions by replacing specific gate operations and optimizing circuit structure. If an exception occurs during simplification, it prints the error message along with the traceback and returns the original instruction string.

Parameters:

qcis (str) – The quantum circuit instruction string to be simplified.

Returns:

The simplified quantum circuit instruction string.

Return type:

str