Open SimoneGasperini opened 1 year ago
The first step is to use Qiskit DAG converters to get the minimal number of layers representation of the circuit. Something like:
from qiskit.converters import circuit_to_dag, dag_to_circuit
layers_data = [dag_to_circuit(layer['graph']).data
for layer in circuit_to_dag(circuit).layers()]
Instead of:
layers_data = circuit.draw(output='text').nodes
For this to work, an adjustment is needed in accessing the attributes of each circuit instruction. In particular:
instruction.op
--> instruction.operation
instruction.qargs
--> instruction.qubits
The most challenging part is then to re-implement the symbolic evaluation algorithm in order to defer all the computation at the level of each layer of the circuit. This would significantly enhance the performance of the method to_lambda
as well: in particular, the time required for transforming a Qiskit circuit into a Python function would scale linearly (and not exponentially) with the circuit depth.
Consider the two circuits in the picture below: in both cases, we have a $CNOT$ gate and an $H$ gate acting in parallel. However, while in the first circuit (left) they are represented in a single "layer", in the second circuit (right) the drawing function moves the $H$ gate in a second "layer".
Since the
qiskit-symb
algorithm is based oncircuit.draw(output='text').nodes
to get the circuit layout before performing the needed linear algebra (see methodQuantumBase._get_circ_data
), the way the second circuit is unfolded is not optimal because gates acting in parallel are split in two different "layers". In particular:$$ \mathrm{circ_1} = H \otimes (I \otimes |0\rangle \langle0| + X \otimes |1\rangle \langle1|) $$
$$ \mathrm{circ_2} = (I \otimes H \otimes I) \cdot (I \otimes I \otimes |0\rangle \langle0| + X \otimes I \otimes |1\rangle \langle1|) $$
The goal is to implement the optimal algorithm to unfold any given circuit to always have its minimal number of "layers". For example, in this case, the second circuit should be represented in the following equivalent form:
$$ \mathrm{circ_2} = I \otimes H \otimes |0\rangle \langle0| + X \otimes H \otimes |1\rangle \langle1| $$