{
"cells": [
{
"cell_type": "markdown",
"id": "345cdcd4fc84356",
"metadata": {},
"source": [
"# Local Computing"
]
},
{
"cell_type": "markdown",
"id": "6ba3240f621f8ae4",
"metadata": {},
"source": [
"This tutorial shows how to simulate a quantum experiment with Perceval running locally, on the user computer.\n",
"\n",
"Up to this point, we have focused on creating circuits.\n",
"It's time to learn how to sample from them or describe their output distribution, from one or many different input states."
]
},
{
"cell_type": "markdown",
"id": "5f1aced257345540",
"metadata": {},
"source": [
"## I. Different layers of simulation\n",
"\n",
"Perceval computation classes are organised in three different layers, each having a specific role and set of features:\n",
"\n",
"A **Processor** relies on a **Simulator** which uses a **Backend** in order to compute results.\n",
"\n",
"Let's describe the intent behind each layer.\n",
"\n",
"
Layer description | Local simulations | Remote computations |
\n",
"\n",
"\n",
"The **processor** layer is the high level interface designed as Perceval entry point to describe and run linear optics experiments.\n",
"It supports the optical hardware capabilities available in actual single photons-based QPU. As such, only sampling and measurements are available\n",
"and not probability amplitudes or state evolutions.\n",
"\n",
"Processors are meant to be used by *quantum algorithm classes* that would use them once or multiple times to achieve a goal. The simplest use\n",
"is to perform sampling in the **Sampler** class. \n",
"This layer is designed so that a user doesn't have to change all their code to swap from a local to a remote computation.\n",
" | \n",
"\n",
"**Processor** is the go-to class for a simulation directly on the user machine. It can simulate any unitary circuit, a small set of non-unitary components\n",
"(TimeDelay, LossChannel) and feed-forward. It simulates real life noise.\n",
"\n",
"It is aimed at being usable by a Perceval *beginner*, up to an expert.\n",
" | \n",
"\n",
"**RemoteProcessor** uses the same syntax to create linear optics experiments. It then connects to a Quandela compatible Cloud provider.\n",
"\n",
"It targets a computation *platform* which can be a remote simulator or an actual QPU. Available features are defined on a per-platform basis (e.g. a remote\n",
"simulator based on a strong simulation algorithm can output exact probabilities, whereas a QPU can only acquire samples).\n",
" |
\n",
"\n",
"\n",
"\n",
"On the receiving end of the experiment, a system has to compute results in order to send them back to the user. The **computation** layer is very diverse as it\n",
"can be a raw \"perfect\" back-end, the implementation of an algorithm simulating a real life setup, or even an actual QPU.\n",
" | \n",
"\n",
"**Simulator classes** are the Swiss army knife of the local simulation algorithms. They are the most versatile, and as such, the harder to use. They target *advanced*\n",
"users.\n",
"\n",
"They can simulate a large variety of noise, any type of input (noisy, superposed, mixed, etc.) and can compute a sampling as well as a state evolution (using strong simulation).\n",
"\n",
"They are built as an onion, where different **Simulator** layers can handle a particular non-unitary problem so that it can be extended by *expert* developers writing\n",
"their own simulator class.\n",
" | \n",
"\n",
"Cloud **platforms** (simulator or actual QPU) embody this layer to produce results.\n",
" |
\n",
"\n",
"\n",
"\n",
"The back-end is the low level method to compute data.\n",
" | \n",
"\n",
"Locally, **Backend classes** are state-of-the-art implementations of published algorithms able to simulate perfect results,\n",
"from a perfect input state in a purely unitary linear optics circuit.\n",
"\n",
"These are optimised algorithms, which can definitely be used by a Perceval *beginner* as long as they only require perfect simulations.\n",
" | \n",
"\n",
"Cloud QPU use their hardware to acquire data.\n",
"\n",
"Cloud simulators rely on algorithms that are specifically optimized for a given classical hardware (multithreaded, GPU, etc.).\n",
" |
\n",
"
\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1305dd0510f1fecb",
"metadata": {},
"outputs": [],
"source": [
"import perceval as pcvl\n",
"from perceval import BS, BasicState, Circuit, Processor\n",
"from perceval.algorithm import Sampler"
]
},
{
"cell_type": "markdown",
"id": "3846837bc2c71ee2",
"metadata": {},
"source": [
"## II. Computing probabilities\n",
"\n",
"For this part, we will take the [Hong-Ou-Mandel](https://en.wikipedia.org/wiki/Hong%E2%80%93Ou%E2%80%93Mandel_effect) experience as an example.\n",
"\n",
"It's one of the simplest experiments and yet it is very useful.\n",
"\n",
"Making two indistinguishable photons, one in each mode, enter one balanced beamsplitter $BS=\\frac{1}{\\sqrt{2}} \\left[\\begin{matrix}1 & 1\\\\1& -1\\end{matrix}\\right]$, we expect the outcome to be:\n",
"\n",
"$$|1,1\\rangle \\mapsto \\frac{|2,0\\rangle - |0,2\\rangle}{\\sqrt{2}} $$\n",
"\n",
"We will show how to verify this in the next steps using the Naive backend to recover the full probability distribution."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "initial_id",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}\\\\\\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}\\end{matrix}\\right]$"
],
"text/plain": [
"