Decomposing Qiskit circuits

In this notebook, we show how Qiskit circuits can be converted into Perceval circuits. To do so, we take the example of a simple gate-based circuit (a circuit producing GHZ states) and we show the translation to a linear optical circuit. We also show the equivalence between the two circuits.

As usual we start by imported the useful libraries. Note that this notebook requires the installation of Qiskit (which can be easiliy done with pip install qiskit).

[1]:
import perceval as pcvl
import perceval.lib.phys as phys
from perceval.converters import QiskitConverter

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

GHZ State generation in Qiskit

We first define the circuit generating GHZ states of 3 qubits with Qiskit. To do so, we first act with a Hadamard gate on qubit 0 to put in superposition of state \(|0\rangle\) and \(|1\rangle\). Then we perform two CNOT gates using qubit 0 as control and qubits 1 and 2 as targets.

[2]:
# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(3)

# Add a H gate on qubit 0
circuit.h(0)

# Add CX (CNOT) gates on control qubit 0 and target qubits 1 and 2
circuit.cx(0, 1)
circuit.cx(0, 2)

# Draw the circuit
circuit.draw()
[2]:
     ┌───┐
q_0: ┤ H ├──■────■──
     └───┘┌─┴─┐  │
q_1: ─────┤ X ├──┼──
          └───┘┌─┴─┐
q_2: ──────────┤ X ├
               └───┘

We display the final state when starting from the input state \(|000\rangle\).

[3]:
# Set the initial state of the simulator to the ground state using from_int
state = Statevector.from_int(0, 2**3)

# Evolve the state by the quantum circuit
state = state.evolve(circuit)

#draw using latex
state.draw('latex')
[3]:
$$\frac{\sqrt{2}}{2} |000\rangle+\frac{\sqrt{2}}{2} |111\rangle$$

Conversion to Perceval

With the use of QiskitConverter, we can transform the Qiskit circuit into a Perceval circuit. It uses 2 modes per qubit and additional modes for ancillary photons to perform deterministically two-qubit gates. Below the first six modes correspond to the three logical qubits (see the ‘Spatial Modes encoding’ paragraph in the ‘Basics’ section of the documentation) of the gate-based circuit above.

The other modes are used to successfully implement two-qubit gates via heralding or post-selection. Heralding employs 4 ancillary modes while post-selection employs 2 ancillary modes. With the option heralded=True in the method .convert on a QiskitConverter object, every CNOT but the last is implemented with a heralding scheme. Here it means that it would add \(4+2\) ancillary modes. The option heralded=True only implements post-selected CNOTs. Here it would mean \(2+2\) ancillary modes. A cautionary warning: post-selected CNOTs do not compose generally.

[4]:
qiskit_converter = QiskitConverter(phys)
quantum_processor = qiskit_converter.convert(circuit, heralded=True)
pcvl.pdisplay(quantum_processor, recursive=True)
Φ_a=pi Φ_d=2*pi Θ=3*pi/4 H Φ_b=pi Φ_d=0 R=0.228 R=0.228 R=0.758 Φ_b=pi Φ_d=0 R=0.758 HERALDED CNOT Φ_b=pi Φ_d=0 R=0.228 R=0.228 R=0.758 Φ_b=pi Φ_d=0 R=0.758 HERALDED CNOT [q_0:0] 1 [q_0:0] [q_0:1] [q_0:1] [q_1:0] 1 [q_1:0] [q_1:1] [q_1:1] [q_2:0] 1 [q_2:0] [q_2:1] [q_2:1] [herald0] 0 [herald0] 0 [herald1] 1 [herald1] 1 [herald2] 0 [herald2] 0 [herald3] 1 [herald3] 1 [herald4] 0 [herald4] 0 [herald5] 1 [herald5] 1 [herald6] 0 [herald6] 0 [herald7] 1 [herald7] 1

With this converted circuit, we can now check that the resulting state is the same as before the conversion.

[5]:
simulator_backend = pcvl.BackendFactory().get_backend("Naive")

_, output_distribution = quantum_processor.run(simulator_backend)
pcvl.pdisplay(output_distribution, precision=1e-2)
state probability
|1,0,1,0,1,0>1/2
|0,1,0,1,0,1>1/2
|1,0,1,0,0,1>0
|1,0,0,1,1,0>0
|0,1,1,0,0,1>0
|0,1,0,1,1,0>0
|1,0,0,1,0,1>0
|0,1,1,0,1,0>0

This circuit can now be converted using a general interferometer decomposition so it can be implemented on a generic photonic chip.

[6]:
u = quantum_processor.circuit.compute_unitary(use_symbolic=False)
[7]:
ub = (pcvl.Circuit(2)
      // phys.BS(theta=pcvl.Parameter("theta"))
      // (0, phys.PS(phi=pcvl.Parameter("φ_a"))))

pc_norm = pcvl.Circuit.decomposition(u, ub, shape="triangle")
pcvl.pdisplay(pc_norm, compact=True, render_size=0.5)
Θ=7*pi/4 Φ=0 Θ=pi/2 Φ=2.451589 Θ=pi/2 Φ=4.126832 Φ=0 Θ=5.751697 Φ=pi/2 Θ=4.000939 Φ=pi/2 Θ=1.937322 Φ=2.970218 Θ=pi/2 Φ=1.742171 Θ=5.138768 Φ=pi/2 Θ=0.787983 Φ=3*pi/2 Θ=5.364659 Φ=3*pi/2 Θ=4.285588 Φ=3*pi/2 Θ=2.142042 Φ=pi/2 Θ=1.252148 Φ=3*pi/2 Θ=2.425285 Φ=pi Θ=3*pi/2 Φ=3.724734 Θ=3*pi/2 Φ=1.456432 Θ=3*pi/2 Φ=1.606489 Θ=6.038759 Φ=2.595932 Θ=3*pi/2 Φ=2.116457 Θ=1.178313 Φ=pi/2 Θ=4.285588 Φ=pi/2 Θ=0.967067 Φ=3*pi/2 Θ=4.255214 Φ=0.22376 Θ=3*pi/2 Φ=4.488629 Θ=5.2267 Φ=3.442639 Θ=pi/2 Φ=5.982139 Φ=pi Θ=pi/2 Φ=4.050745 Θ=3*pi/2 Φ=6.076092 Θ=pi/2 Φ=1.236768 Θ=2.643793 Φ=pi Θ=3*pi/2 Φ=0.763438 Θ=5.903077 Φ=1.088905 Θ=3*pi/2 Φ=0.481891 Θ=1.926074 Φ=5.358426 Θ=3*pi/2 Φ=4.2908 Θ=3*pi/2 Φ=3.786773 Θ=pi/2 Φ=5.956187 Θ=3*pi/2 Φ=5.740554 Θ=0.955317 Φ=pi Θ=1.072997 Φ=3*pi/2 Θ=5.067666 Φ=6.129562 Θ=3*pi/2 Φ=4.866012 Θ=5.092497 Φ=1.756105 Θ=pi/2 Φ=2.902757 Θ=3*pi/2 Φ=3.171965 Θ=pi/2 Φ=1.963735 Θ=pi/2 Φ=2.771808 Θ=4*pi/3 Φ=pi Θ=4.096909 Φ=5.073798 Θ=pi/2 Φ=5.125491 Θ=pi/2 Φ=5.508674 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13