qrisp.QuantumCircuit.transpile#

QuantumCircuit.transpile(transpilation_level=inf, **qiskit_kwargs)[source]#

Transpiles the QuantumCircuit in the sense that there are no longer any synthesized gate objects. Furthermore, we can call the Qiskit transpiler by supplying keyword arguments.

The Qiskit transpiler is not called, if no keyword arguments are given.

Parameters:
**qiskit_kwargs

Keyword arguments for the Qiskit transpiler.

Returns:
QuantumCircuit

The transpiled QuantumCircuit.

Examples

We create a QuantumCircuit and append a synthesized gate. Afterwards we transpile to a given set of basis gates using the Qiskit transpiler:

>>> from qrisp import QuantumCircuit
>>> qc = QuantumCircuit(3)
>>> qc.mcx([0,1], 2)
>>> print(qc)
qb_0: ──■──
        │
qb_1: ──■──
      ┌─┴─┐
qb_2: ┤ X ├
      └───┘
>>> print(qc.transpile(basis_gates = ["cx", "rz", "sx"]))
global phase: 9π/8
      ┌──────────┐                 ┌───┐┌─────────┐   ┌───┐   ┌──────────┐┌───┐»
qb_1: ┤ Rz(-π/4) ├─────────────────┤ X ├┤ Rz(π/4) ├───┤ X ├───┤ Rz(-π/4) ├┤ X ├»
      ├──────────┤                 └─┬─┘└─────────┘   └─┬─┘   └──────────┘└─┬─┘»
qb_2: ┤ Rz(-π/4) ├───────────────────┼───────■──────────■──────────■────────┼──»
      ├─────────┬┘┌────┐┌─────────┐  │     ┌─┴─┐   ┌─────────┐   ┌─┴─┐      │  »
qb_3: ┤ Rz(π/2) ├─┤ √X ├┤ Rz(π/2) ├──■─────┤ X ├───┤ Rz(π/4) ├───┤ X ├──────■──»
      └─────────┘ └────┘└─────────┘        └───┘   └─────────┘   └───┘         »
«      ┌─────────┐┌───┐
«qb_1: ┤ Rz(π/4) ├┤ X ├────────────
«      └─────────┘└─┬─┘
«qb_2: ─────────────■──────────────
«      ┌─────────┐┌────┐┌─────────┐
«qb_3: ┤ Rz(π/4) ├┤ √X ├┤ Rz(π/2) ├
«      └─────────┘└────┘└─────────┘

One can also transpile a specific composite gate in a QuantumCircuit, if desired. A Quantum Phase Estimation circuit also contains a QFT_dg gate.

>>> from qrisp import p, QuantumVariable, QPE, multi_measurement, h
>>> import numpy as np
>>>
>>> def U(qv):
    >>> x = 0.5
    >>> y = 0.125
>>> p(x*2*np.pi, qv[0])
>>> p(y*2*np.pi, qv[1])
>>> 
>>> qv = QuantumVariable(2)
>>>
>>> h(qv)
>>> 
>>> res = QPE(qv, U, precision = 3)
>>>
>>> print(qv.qs.compile())

To transpile just QFT_dg in the compiled QuantumCircuit,

>>> test_circuit = qv.qs.compile()
>>>
>>> def transpile_predicate(op):
>>>    if op.name == "QFT_dg":
>>>        return True
>>>    else:
>>>        return False
>>>    
>>> transpiled_qc = test_circuit.transpile(transpile_predicate = transpile_predicate)
>>>
>>> print(transpiled_qc)