Skip to content

Commit

Permalink
extended the IBM example
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian S. Steiger committed Apr 20, 2017
1 parent 468e8d7 commit 1f60033
Showing 1 changed file with 217 additions and 11 deletions.
228 changes: 217 additions & 11 deletions examples/ibm_entangle.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"outputs": [],
"source": [
"import projectq.setups.ibm\n",
"import projectq.setups.ibm # Imports the default compiler to map to IBM QE\n",
"from projectq.backends import IBMBackend\n",
"from projectq.ops import Measure, Entangle\n",
"from projectq import MainEngine"
Expand Down Expand Up @@ -116,14 +116,14 @@
"Running code...\n",
"Waiting for results...\n",
"Done.\n",
"00000 with p = 0.4755859375*\n",
"10000 with p = 0.0048828125\n",
"01000 with p = 0.0107421875\n",
"11000 with p = 0.0322265625\n",
"00100 with p = 0.0078125\n",
"10100 with p = 0.021484375\n",
"01100 with p = 0.033203125\n",
"11100 with p = 0.4140625\n"
"00000 with p = 0.4677734375*\n",
"10000 with p = 0.00390625\n",
"01000 with p = 0.009765625\n",
"11000 with p = 0.0224609375\n",
"00100 with p = 0.005859375\n",
"10100 with p = 0.0263671875\n",
"01100 with p = 0.0322265625\n",
"11100 with p = 0.431640625\n"
]
}
],
Expand Down Expand Up @@ -164,13 +164,219 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"source": [
"## Inspecting the gates required for a 5-qubit entangle on IBM QE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a new compiler with a command printer as a back-end:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from projectq.backends import CommandPrinter\n",
"engine2 = MainEngine(CommandPrinter())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Allocate a quantum register of 5 qubits:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"qureg2 = engine2.allocate_qureg(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Entangle quantum register (and run the circuit):\n",
"\n",
"A 5-qubit entangle operation requires one H gate and 4 CNOTs\n",
"if the qubit chip would support CNOTs between arbitrary qubits:\n",
"```\n",
"H | Qubit[0]\n",
"CX | ( Qubit[0], Qubit[1] )\n",
"CX | ( Qubit[0], Qubit[2] )\n",
"CX | ( Qubit[0], Qubit[3] )\n",
"CX | ( Qubit[0], Qubit[4] )\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Allocate | Qubit[0]\n",
"Allocate | Qubit[1]\n",
"H | Qubit[1]\n",
"CX | ( Qubit[1], Qubit[0] )\n",
"Allocate | Qubit[2]\n",
"H | Qubit[2]\n",
"CX | ( Qubit[2], Qubit[0] )\n",
"Allocate | Qubit[3]\n",
"H | Qubit[3]\n",
"CX | ( Qubit[3], Qubit[0] )\n",
"Allocate | Qubit[4]\n",
"H | Qubit[4]\n",
"CX | ( Qubit[4], Qubit[0] )\n",
"H | Qubit[0]\n",
"H | Qubit[1]\n",
"H | Qubit[2]\n",
"H | Qubit[3]\n",
"H | Qubit[4]\n"
]
}
],
"source": [
"Entangle | qureg2\n",
"engine2.flush()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As IBM's QE chip does not support CNOTs between arbitrary qubits, the compiler automatically\n",
"changes the abstract entangle circuit to an equivalent circuit which takes\n",
"into account the hardware constraints of the IBM QE chip. The resulting circuit is shown above as the output of the command printer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simulating a circuit for the IBM QE chip"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a new compiler with our high-performance quantum simulator as a back-end:\n",
"\n",
">### Note \n",
">Our Simulator can simulate much larger circuits but here we imported the compiler for the IBM QE chip:\n",
"```\n",
"import projectq.setups.ibm\n",
"```\n",
"> and therefore the compiler will only allow circuits which can be mapped to the IBM QE chip. Running larger simulations is easily possible by using the default compiler (see other example codes)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from projectq.backends import Simulator\n",
"engine3 = MainEngine(Simulator())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Allocate a 5-qubit quantum register and apply an entangle operations:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"qureg3 = engine3.allocate_qureg(5)\n",
"Entangle | qureg3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Measure the quantum register and run the circuit:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Measure | qureg3\n",
"engine3.flush()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Output the measurement result:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 1, 1, 1, 1]\n"
]
}
],
"source": [
"print([int(q) for q in qureg3])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
Expand Down

0 comments on commit 1f60033

Please sign in to comment.