{
"cells": [
{
"cell_type": "markdown",
"id": "606547321bc52b8c",
"metadata": {},
"source": [
"# cQASM converter"
]
},
{
"cell_type": "markdown",
"id": "b65ae950d965af5c",
"metadata": {},
"source": [
"As usual, we start by importing the needed libraries. Note that this notebook requires the installation of cQASM (which can be easily done with `pip install libqasm`). This repository can also be installed with the command: `pip install .[CQASM-bridge]` to automatically install cQASM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f28da1a524c48c8b",
"metadata": {},
"outputs": [],
"source": [
"from perceval_interop import CQASMConverter\n",
"from perceval import pdisplay"
]
},
{
"cell_type": "markdown",
"id": "7de0b29a4bd45702",
"metadata": {},
"source": [
"Then we write our cQASM program."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8f72c9eb15aed3f4",
"metadata": {},
"outputs": [],
"source": [
"cqasm_program = \"\"\"\n",
"version 3\n",
"qubit[2] q\n",
"H q[0]\n",
"CNOT q[0], q[1]\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"id": "fff43a202cf3530f",
"metadata": {},
"source": [
"We convert it with the Perceval CQASMConverter."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "47da8c1e2250ed58",
"metadata": {},
"outputs": [],
"source": [
"perceval_processor = CQASMConverter().convert(cqasm_program, use_postselection=False)"
]
},
{
"cell_type": "markdown",
"id": "e6de7c73b2bc4340",
"metadata": {},
"source": [
"The output probabilities can now be computed using Perceval."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6f2490487920f8d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"results: {\n",
"\t|1,0,1,0>: 0.5000000000000002\n",
"\t|0,1,0,1>: 0.4999999999999999\n",
"}\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = perceval_processor.probs()['results']\n",
"print('results:', r)\n",
"pdisplay(perceval_processor)"
]
},
{
"cell_type": "markdown",
"id": "c0a96be6ff24a6b",
"metadata": {},
"source": [
"The 1.0 version of cQASM is also supported, as shown in the example below.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a4978c2b86d27241",
"metadata": {},
"outputs": [],
"source": [
"# version 1.0\n",
"cqasm_program = f\"\"\"\n",
"version 1.0\n",
"\n",
"# a basic cQASM example\n",
"qubits 2\n",
"\n",
".prepare\n",
"prep_z q[0:1]\n",
"\n",
".entangle\n",
"H q[0]\n",
"CNOT q[0], q[1]\n",
"\n",
".measurement\n",
"measure_all\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dba923f2114d77d0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"results: {\n",
"\t|1,0,1,0>: 0.5000000000000002\n",
"\t|0,1,0,1>: 0.4999999999999999\n",
"}\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"perceval_processor = CQASMConverter().convert(cqasm_program, use_postselection=False)\n",
"\n",
"r = perceval_processor.probs()['results']\n",
"print('results:', r)\n",
"pdisplay(perceval_processor)"
]
},
{
"cell_type": "markdown",
"id": "ece911ebed01fbc8",
"metadata": {},
"source": [
"The cQASM program can also be retrieved from a file by using `CQASMConverter().convert(path_to_cqasm_program)`."
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}