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 importing the needed libraries. Note that this notebook requires the installation of Qiskit (which can be easiliy done with
pip install qiskit).
import perceval as pcvl from perceval.components import catalog from perceval.converters import QiskitConverter from perceval.algorithm import Analyzer, Sampler 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.
# 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()
┌───┐ q_0: ┤ H ├──■────■── └───┘┌─┴─┐ │ q_1: ─────┤ X ├──┼── └───┘┌─┴─┐ q_2: ──────────┤ X ├ └───┘
We display the final state when starting from the input state \(|000\rangle\).
# 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')
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
use_postselection=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
use_postselection=False only implements heralded CNOTs. Here it would mean \(4+4\) ancillary modes. Note: the
use_postselection option is
True by default.
qiskit_converter = QiskitConverter(catalog, backend_name="Naive") quantum_processor = qiskit_converter.convert(circuit, use_postselection=True) pcvl.pdisplay(quantum_processor, recursive=True)
With this converted circuit, we can now check that the resulting state is the same as before the conversion. By default, the input is the logical state \(|000\rangle_L\). Note that where Qiskit displays state in the order \(|q_2q_1q_0\rangle_L\), Perceval uses the reverse order \(|q_0q_1q_2\rangle_L\), but still shown as Fock states. Here, it doesn’t change anything since we end with only \(|000\rangle_L\) and \(|111\rangle_L\) states.
# Not necessary here quantum_processor.with_input(pcvl.LogicalState([0,0,0])) sampler = Sampler(quantum_processor) output_distribution = sampler.probs()["results"] pcvl.pdisplay(output_distribution, precision=1e-2, max_v = 4)
This circuit can now be converted using a general interferometer decomposition so it can be implemented on a generic photonic chip.
u = quantum_processor.linear_circuit().compute_unitary(use_symbolic=False)
ub = (pcvl.Circuit(2) // pcvl.BS(theta=pcvl.Parameter("theta")) // (0, pcvl.PS(phi=pcvl.Parameter("φ_a")))) pc_norm = pcvl.Circuit.decomposition(u, ub, shape="triangle") pcvl.pdisplay(pc_norm, compact=True, render_size=0.5)
A cnot based on CZ
Another interesting example we can explore is how to build a cnot from a CZ gate using qiskit then convert it to Perceval. We will apply the following equivalence:
The code in Qiskit:
circuit = QuantumCircuit(2) # Add (CNOT) built using equivalence with H-CZ-H circuit.h(1) circuit.cz(0, 1) circuit.h(1) # Draw the circuit circuit.draw()
q_0: ──────■────── ┌───┐ │ ┌───┐ q_1: ┤ H ├─■─┤ H ├ └───┘ └───┘
Then we call the converter like the previous example
state = Statevector.from_int(0, 2**3) state = state.evolve(circuit) qiskit_converter = QiskitConverter(catalog, backend_name="SLOS") quantum_processor = qiskit_converter.convert(circuit) # pcvl.pdisplay(quantum_processor, recursive=True) # the perceval processor can be displayed at this point if needed input_states = [pcvl.BasicState([1, 0, 1, 0]), pcvl.BasicState([1, 0, 0, 1]), pcvl.BasicState([0, 1, 1, 0]), pcvl.BasicState([0, 1, 0, 1])] analyzer = Analyzer(quantum_processor, input_states) pcvl.pdisplay(analyzer)
This is the truth table of a CNOT gate
Controlflow operations such as measurement operator in the qiskit ciruit or
qiskit.circuit.QuantumCircuit.if_testare not supported.
Custom gates are also not supported at the moment (see Issue#201).