{
"cells": [
{
"cell_type": "markdown",
"id": "b947311a80a5d790",
"metadata": {},
"source": [
"# MyQLM converter"
]
},
{
"cell_type": "markdown",
"id": "6a6452e23bca09bb",
"metadata": {},
"source": [
"## Minimal code\n",
"\n",
"This section demonstrates the basic usage of the MyQLM converter with a simple example. We'll create a single-qubit circuit that applies an X gate and convert it to a Perceval photonic processor."
]
},
{
"cell_type": "markdown",
"id": "4e5d7477a6e6214d",
"metadata": {},
"source": [
"Note that this notebook requires the installation of MyQLM (which can be easily done with `pip install myqlm`). This repository can also be installed with the command: `pip install .[MyQLM-bridge]` to automatically install MyQLM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b08e8c1d14b85814",
"metadata": {},
"outputs": [],
"source": [
"# Import required libraries\n",
"import qat.lang.AQASM as qataqasm\n",
"import perceval as pcvl\n",
"from perceval_interop import MyQLMConverter"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "50ab8f3daca00779",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Create a MyQLM quantum program\n",
"qprog = qataqasm.Program()\n",
"\n",
"# Allocate quantum bits\n",
"qbits = qprog.qalloc(1) # Allocate 1 qubit\n",
"\n",
"# Apply quantum gate (X gate = NOT gate)\n",
"qprog.apply(qataqasm.X, qbits[0]) # Apply X gate to flip |0⟩ → |1⟩\n",
"\n",
"# Convert program to MyQLM circuit\n",
"myqlm_circuit = qprog.to_circ()\n",
"myqlm_circuit.display()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d29f8797520121cd",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert MyQLM circuit to Perceval processor\n",
"myqlm_converter = MyQLMConverter(backend_name=\"Naive\")\n",
"converted_processor = myqlm_converter.convert(myqlm_circuit, use_postselection=True)\n",
"\n",
"pcvl.pdisplay(converted_processor, recursive=True)"
]
},
{
"cell_type": "markdown",
"id": "e2edcc9e9ca28502",
"metadata": {},
"source": [
"## Circuit to generate GHZ State"
]
},
{
"cell_type": "markdown",
"id": "3ea6d86bebebef7a",
"metadata": {},
"source": [
"In this section, we show how circuits from myQLM can be converted into Perceval circuits. To do so, we take the example of a simple gate-based circuit producing GHZ states. We then show the translation to a linear optical circuit. We also show the equivalence between the two circuits (gate-based and perceval)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "68b895a592bb7204",
"metadata": {},
"outputs": [],
"source": [
"from perceval.algorithm import Sampler"
]
},
{
"cell_type": "markdown",
"id": "9dd4f051bc6dad6a",
"metadata": {},
"source": [
"### Conversion from MyQLM Circuit to Perceval"
]
},
{
"cell_type": "markdown",
"id": "4fd8a2cb43aab284",
"metadata": {},
"source": [
"Analogous to `QiskitConverter`, employ a `MyQLMConverter` object to convert a MyQLM circuit to a Perceval processor with each qubit of the circuit represented by 2 modes and additional modes for ancillary photons to perform deterministically two-qubit gates. (Read in the qiskit converter notebook for a discussion on how ancillary modes are set)."
]
},
{
"cell_type": "markdown",
"id": "2d793a1f8defe725",
"metadata": {},
"source": [
"### Circuit to generate GHZ State in MyQLM"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4e92e614aedad2dc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Create a myqlm program\n",
"qprog = qataqasm.Program()\n",
"\n",
"# Allocate qbits to the Program\n",
"qbits = qprog.qalloc(3)\n",
"\n",
"# Add gates\n",
"qprog.apply(qataqasm.H, qbits[0])\n",
"qprog.apply(qataqasm.CNOT, qbits[0], qbits[1])\n",
"qprog.apply(qataqasm.CNOT, qbits[0], qbits[2])\n",
"\n",
"# Convert program to myqlm circuit\n",
"myqlm_circuit = qprog.to_circ()\n",
"\n",
"myqlm_circuit.display()"
]
},
{
"cell_type": "markdown",
"id": "855a6c0f1c7b9039",
"metadata": {},
"source": [
"### Conversion of Myqlm circuit to Perceval"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "87da999447955253",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myqlm_converter = MyQLMConverter(backend_name=\"Naive\")\n",
"converted_processor = myqlm_converter.convert(myqlm_circuit, use_postselection=True)\n",
"pcvl.pdisplay(converted_processor, recursive=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "175c8583e54528b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"state | probability |
\n",
"\n",
"\n",
"|0,1,0,1,0,1> | 1/2 |
\n",
"|1,0,1,0,1,0> | 1/2 |
\n",
"\n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# computing results with converted processor from myqlm\n",
"converted_processor.with_input(pcvl.LogicalState([0,0,0]))\n",
"\n",
"sampler = Sampler(converted_processor)\n",
"\n",
"output_distribution_myqlm_pcvl = sampler.probs()[\"results\"]\n",
"pcvl.pdisplay(output_distribution_myqlm_pcvl, precision=1e-2, max_v = 4)"
]
},
{
"cell_type": "markdown",
"id": "4598a7894f618fb4",
"metadata": {},
"source": [
"Note: The result is exactly the same as previously obtained from converter from Qiskit and is also the expected result."
]
},
{
"cell_type": "markdown",
"id": "8ef0e7dc8f3c129a",
"metadata": {},
"source": [
"### Few remarks\n",
"- Only the following gates are supported:\n",
" - 1-Qubit gates\n",
" - 2-Qubits gates: CNOT, CSIGN, SWAP\n",
" - 3-Qubits gate: Toffoli\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}