Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/tomography.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ Finally, we analyze our data with one of the analysis routines::
[-0.1869-0.0077j -0.1794-0.0188j -0.169 -0.0169j 0.2202-0.j ]]
Purity = (0.6889520199999999+4.597017211338539e-17j)

Debugger
~~~~~~~~~

The above steps can be automated to create a basic debugger that can be used to
peek into the state of a program running on a qc. This can be done using the
tomographize function::

rho = tomographize(qc, program, qubits, pauli_num=10, t_type="compressed_sensing")

API Reference
-------------
Expand All @@ -96,4 +104,5 @@ API Reference
iterative_mle_state
project_density_matrix
estimate_variance
tomographize

235 changes: 235 additions & 0 deletions examples/tomography_debugger.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tomography Debugger\n",
"State tomography involves measuring a quantum state repeatedly in the bases given by `itertools.product(['X', 'Y', 'Z], repeat=n_qubits)`. From these measurements, we can reconstruct a density matrix $\\rho$ using a varaiety of methods described in forest.benchmarking.tomography under the heading \"state tomography\". This is all done automaticly in using the forest.benchmarking.tomography.tomographize function allowing it to be use effectivly as a quantum debugger."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import time\n",
"\n",
"from pyquil import Program, get_qc\n",
"from pyquil.gates import *\n",
"from forest.benchmarking.tomography import tomographize"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Construct a state with a `Program`\n",
"We'll construct a two-qubit graph state by Hadamarding all qubits and then applying a controlled-Z operation across edges of our graph. In the two-qubit case, there's only one edge. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qubits = [0, 1]\n",
"\n",
"program = Program()\n",
"for qubit in qubits:\n",
" program += H(qubit)\n",
"program += CZ(qubits[0], qubits[1])\n",
"program += RY(-np.pi/2, qubits[0])\n",
"program += X(qubits[1])\n",
"program += CNOT(qubits[1], qubits[0])\n",
"\n",
"print(program)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run the tomography debugger and print output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qc = get_qc('%dq-qvm' % len(qubits))\n",
"\n",
"\n",
"start_linear = time.time()\n",
"m = 10\n",
"rho_linear = tomographize(qc, program, qubits, pauli_num=10, t_type=\"linear_inv\")\n",
"end_linear = time.time() - start_linear\n",
"\n",
"print(\"Linear tomography took %gs\" % end_linear)\n",
"print(\"Recovered density matrix:\\n\")\n",
"print(rho_linear)\n",
"\n",
"start_compressed = time.time()\n",
"rho_compressed = tomographize(qc, program, qubits, pauli_num=10, t_type=\"compressed_sensing\")\n",
"end_compressed = time.time() - start_compressed\n",
"print(\"Compressed tomography took %gs\" % end_compressed)\n",
"print(\"Recovered density matrix:\\n\")\n",
"print(rho_compressed)\n",
"\n",
"start_lasso = time.time()\n",
"rho_lasso = tomographize(qc, program, qubits, pauli_num=10, t_type=\"lasso\")\n",
"end_lasso = time.time() - start_lasso\n",
"print(\"Compressed tomography took %gs\" % end_lasso)\n",
"print(\"Recovered density matrix:\\n\")\n",
"print(rho_lasso)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compare results to true output obtained using wavefunction simulator"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pyquil.api import WavefunctionSimulator\n",
"wf_sim = WavefunctionSimulator()\n",
"wf = wf_sim.wavefunction(program)\n",
"psi = wf.amplitudes\n",
"\n",
"rho_true = np.outer(psi, psi.T.conj())\n",
"print(np.around(rho_true, decimals=3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize using Hinton plots"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"from forest.benchmarking.plotting import hinton\n",
"fig, (ax1, ax2, ax3) = plt.subplots(1, 3)\n",
"hinton(rho_true, ax=ax1)\n",
"hinton(rho_linear, ax=ax2)\n",
"hinton(rho_compressed, ax=ax3)\n",
"ax1.set_title('Analytical Linear')\n",
"ax2.set_title('Estimated Linear')\n",
"ax3.set_title('Estimated Compressed')\n",
"fig.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculate matrix norm between true and estimated rho"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Linear norm:\")\n",
"print(np.linalg.norm(rho_linear - rho_true))\n",
"print(\"Compressed norm:\")\n",
"print(np.linalg.norm(rho_compressed - rho_true))\n",
"print(\"Lasso norm:\")\n",
"print(np.linalg.norm(rho_lasso - rho_true))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot graph of results for various measurement values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"max_pauli_num = 4 ** len(qubits)\n",
"num_trials = 5\n",
"\n",
"linear_norms = []\n",
"compressed_norms = []\n",
"\n",
"print(\"Analyzing performance of linear vs. compressed on program:\")\n",
"print(program)\n",
"\n",
"for i in range(1, max_pauli_num):\n",
" print(\"Running iteration %d/%d\" % (i, max_pauli_num))\n",
" linear_norm_mean = 0.0\n",
" compressed_norm_mean = 0.0\n",
" for j in range(num_trials):\n",
" rho_linear = tomographize(qc, program, qubits, pauli_num=i, t_type=\"linear_inv\")\n",
" rho_compressed = tomographize(qc, program, qubits, pauli_num=i, t_type=\"compressed_sensing\")\n",
" linear_norm_mean += np.linalg.norm(rho_linear - rho_true)\n",
" compressed_norm_mean += np.linalg.norm(rho_compressed - rho_true)\n",
" \n",
" linear_norm_mean /= num_trials\n",
" compressed_norm_mean /= num_trials\n",
" \n",
" linear_norms.append(linear_norm_mean)\n",
" compressed_norms.append(compressed_norm_mean)\n",
"\n",
"plt.plot(linear_norms, label='linear')\n",
"plt.plot(compressed_norms, label='compressed')\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading