138 lines (137 with data), 4.2 kB
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"# TShift is X https://docs.pennylane.ai/en/stable/code/api/pennylane.TShift.html\n",
"# TAdd is CX of TShift https://docs.pennylane.ai/en/stable/code/api/pennylane.TAdd.html\n",
"# THadamard is a 3x3 Hadamard https://docs.pennylane.ai/en/stable/code/api/pennylane.THadamard.html?highlight=thadamard"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value of a is [2 2 2 0 1 1 1 1 1 1 1 1 1 1 2]\n"
]
}
],
"source": [
"import pennylane as qml\n",
"\n",
"dev = qml.device(\"default.qutrit\", wires=16, shots=1)\n",
"\n",
"@qml.qnode(dev)\n",
"def circuit():\n",
"\n",
" # Changed: We initialize to |0001> \n",
" qml.TShift(wires = 15)\n",
"\n",
" # Changed: We run the THadamard\n",
" for i in range(16):\n",
" qml.THadamard(wires = i)\n",
"\n",
"# Changed: The oracle in charge of encoding a hidden \"a\" value.\n",
" qml.TAdd(wires = [0,15])\n",
" qml.TAdd(wires = [0,15])\n",
" qml.TAdd(wires = [1,15])\n",
" qml.TAdd(wires = [1,15])\n",
" qml.TAdd(wires = [2,15])\n",
" qml.TAdd(wires = [2,15])\n",
" # qml.TAdd(wires = [3,15])\n",
" qml.TAdd(wires = [4,15])\n",
" qml.TAdd(wires = [5,15])\n",
" qml.TAdd(wires = [6,15])\n",
" qml.TAdd(wires = [7,15])\n",
" qml.TAdd(wires = [8,15])\n",
" qml.TAdd(wires = [9,15])\n",
" qml.TAdd(wires = [10,15])\n",
" qml.TAdd(wires = [11,15])\n",
" qml.TAdd(wires = [12,15])\n",
" qml.TAdd(wires = [13,15])\n",
" qml.TAdd(wires = [14,15])\n",
" qml.TAdd(wires = [14,15]) \n",
" \n",
"# Changed: We run the THadamard again\n",
" for i in range(15):\n",
" qml.THadamard(wires = i)\n",
"\n",
" # Changed: We measure the first 3 qutrits\n",
" return qml.sample(wires = range(15))\n",
"\n",
"a = circuit()\n",
"\n",
"print(f\"The value of a is {a}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Conclusion\n",
"==========\n",
"\n",
"In this demo, we have practised the use of basic qutrit gates such as\n",
"TShift or THadamard by applying the Bernstein--Vazirani algorithm. In\n",
"this case, the generalization has been straightforward and we have found\n",
"that it makes mathematical sense, but we cannot always substitute qubit\n",
"gates for qutrit gates as we have seen in the demo. To give an easy\n",
"example of this, we know the property that $X = HZH$, but it turns out\n",
"that this property does not generalize! The general property is actually\n",
"$X = H^{\\dagger}ZH$. In the case of qubits it holds that\n",
"$H = H^{\\dagger}$, but in other dimensions it does not. I invite you to\n",
"continue practising with other types of algorithms. For instance, will\n",
"the [Deutsch--Jozsa\n",
"algorithm](https://en.wikipedia.org/wiki/Deutsch–Jozsa_algorithm)\n",
"generalize well? Take a pen and paper and check it out!\n",
"\n",
"References\n",
"==========\n",
"\n",
"About the author\n",
"================\n",
"https://pennylane.ai/qml/demos/tutorial_qutrits_bernstein_vazirani.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}