Skip to content

translate ¤

Translate single- and two-qubit gates in the quantum circuit into basis gates.

Classes:

Name Description
TranslateToBasisGates

Transpiler pass for converting quantum gates to hardware-specific basis gates.

TranslateToBasisGates(convert_single_qubit_gate_to_u: bool = True, two_qubit_gate_basis: Literal['cz', 'cx', 'iswap'] = 'cz') ¤

Bases: TranspilerPass

Transpiler pass for converting quantum gates to hardware-specific basis gates.

Parameters:

Name Type Description Default
TranspilerPass class

The base class that provides the structure for the transpiler pass.

required

Initializes the TranslateToBasisGates class with the specified settings.

Parameters:

Name Type Description Default
convert_single_qubit_gate_to_u bool

If True, converts all single-qubit gates into U3 gates. Defaults to True.

True
two_qubit_gate_basis Literal['cz', 'cx']

Specifies the basis gate for two-qubit gate decomposition. Defaults to 'cz'.

'cz'

Methods:

Name Description
run

Translate all gates in the quantum circuit into a specified basis gate set.

Source code in quark/circuit/translate.py
52
53
54
55
56
57
58
59
60
61
def __init__(self,convert_single_qubit_gate_to_u:bool=True, two_qubit_gate_basis: Literal['cz','cx','iswap']='cz'):
    """Initializes the TranslateToBasisGates class with the specified settings.

    Args:
        convert_single_qubit_gate_to_u (bool, optional): If True, converts all single-qubit gates into U3 gates. Defaults to True.
        two_qubit_gate_basis (Literal['cz','cx'], optional): Specifies the basis gate for two-qubit gate decomposition. Defaults to 'cz'.
    """
    super().__init__()
    self.convert_single_qubit_gate_to_u = convert_single_qubit_gate_to_u
    self.two_qubit_gate_basis = two_qubit_gate_basis

run(qc: QuantumCircuit) -> QuantumCircuit ¤

Translate all gates in the quantum circuit into a specified basis gate set.

Parameters:

Name Type Description Default
qc QuantumCircuit

The input quantum circuit to be translated.

required

Returns:

Name Type Description
QuantumCircuit QuantumCircuit

A new quantum circuit where all gates are expressed using the chosen basis gate set.

Source code in quark/circuit/translate.py
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def run(self, qc: QuantumCircuit) -> QuantumCircuit:
    r"""Translate all gates in the quantum circuit into a specified basis gate set.

    Args:
        qc (QuantumCircuit): The input quantum circuit to be translated.

    Returns:
        QuantumCircuit: A new quantum circuit where all gates are expressed using the chosen basis gate set.
    """
    new_qc = qc.deepcopy()

    new = []
    for gate_info in qc.gates:
        gate = gate_info[0]
        if gate in one_qubit_gates_available.keys():
            if self.convert_single_qubit_gate_to_u:
                gate_matrix = gate_matrix_dict[gate]
                theta,phi,lamda,_ = u3_decompose(gate_matrix)
                new.append(('u',theta,phi,lamda,gate_info[-1]))
            else:
                new.append(gate_info)
        elif gate in one_qubit_parameter_gates_available.keys():
            if self.convert_single_qubit_gate_to_u:
                if gate == 'u':
                    new.append(gate_info)
                elif gate == 'r':
                    theta,phi,qubit = gate_info[1:]
                    new.append(('u',theta,phi-np.pi/2,np.pi/2-phi,qubit))
                else:
                    gate_matrix = gate_matrix_dict[gate](*gate_info[1:-1])
                    theta,phi,lamda,_ = u3_decompose(gate_matrix)
                    new.append(('u',theta,phi,lamda,gate_info[-1]))
            else:
                new.append(gate_info)
        elif gate in two_qubit_gates_available.keys():
            if gate in ['cz']:
                if self.two_qubit_gate_basis in ['cx','cz']:
                    new += [gate_info]
                else:
                    _cz = cz_decompose(gate_info[1],gate_info[2],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
                    new += _cz
            elif gate in ['cx', 'cnot']:
                _cx = cx_decompose(gate_info[1],gate_info[2],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
                new += _cx
            elif gate in ['swap']:
                _swap = swap_decompose(gate_info[1],gate_info[2],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
                new += _swap
            elif gate in ['iswap']:
                _iswap = iswap_decompose(gate_info[1], gate_info[2],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
                new += _iswap
            elif gate in ['cy']:
                _cy = cy_decompose(gate_info[1], gate_info[2],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
                new += _cy
            else:
                raise(TypeError(f'Input {gate} gate is not support now. Try kak please'))       
        elif gate in two_qubit_parameter_gates_available.keys():
            if gate == 'rxx':
                new += rxx_decompose(*gate_info[1:],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
            elif gate == 'ryy':
                new += ryy_decompose(*gate_info[1:],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
            elif gate == 'rzz':
                new += rzz_decompose(*gate_info[1:],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
            elif gate == 'cp':
                new += cp_decompose(*gate_info[1:],self.convert_single_qubit_gate_to_u,self.two_qubit_gate_basis)
        elif gate in functional_gates_available.keys():
            new.append(gate_info)
        else:
            raise(TypeError(f'Input {gate} gate is not support to basic gates now.'))

    new_qc.gates = new
    print(f'Mapping to basic gates done ! {self.two_qubit_gate_basis}')
    return new_qc