Skip to content

Instantly share code, notes, and snippets.

@jond01
Last active July 21, 2021 16:24
Show Gist options
  • Save jond01/c942a7358e27e99aa6a68ea7ef42fe65 to your computer and use it in GitHub Desktop.
Save jond01/c942a7358e27e99aa6a68ea7ef42fe65 to your computer and use it in GitHub Desktop.
Compare qsharp-integrations translated U gate (in Q#) with Qiskit's U gate (QASM)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f9c5010e-ab54-4ffa-9415-817133675953",
"metadata": {},
"outputs": [],
"source": [
"import functools\n",
"from sympy import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3f78a76f-d97c-4c0c-a391-33476737da41",
"metadata": {},
"outputs": [],
"source": [
"theta, phi, lmd = symbols(r\"\\theta \\phi \\lambda\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "78a425f0-ab02-4f9f-8698-47e144342ce5",
"metadata": {},
"outputs": [],
"source": [
"Id = Matrix([\n",
" [1, 0],\n",
" [0, 1]\n",
"])\n",
"\n",
"X = Matrix([\n",
" [0, 1],\n",
" [1, 0]\n",
"])\n",
"\n",
"Y = Matrix([\n",
" [0 , -1j],\n",
" [1j, 0]\n",
"])\n",
"\n",
"Z = Matrix([\n",
" [1, 0],\n",
" [0, -1]\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a86cd12c-456b-4ebe-9d91-57b8fae2591b",
"metadata": {},
"outputs": [],
"source": [
"def R(angle, op):\n",
" return exp(-I * angle * op / 2)\n",
"\n",
"Rx = functools.partial(R, op=X)\n",
"Ry = functools.partial(R, op=Y)\n",
"Rz = functools.partial(R, op=Z)\n",
"gp = functools.partial(R, op=Id) # global phase"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "085bc918-722d-4346-8175-0a7c96a24dd1",
"metadata": {},
"outputs": [],
"source": [
"def U_qiskit(theta, phi, lmd):\n",
" \"\"\"\n",
" https://qiskit.org/documentation/stubs/qiskit.circuit.library.UGate.html\n",
" Matrix Representation\n",
" \"\"\"\n",
" return Matrix([\n",
" [cos(theta / 2), -exp(I * lmd) * sin(theta / 2)],\n",
" [exp(I * phi) * sin(theta / 2), exp(I * (phi + lmd)) * cos(theta / 2)]\n",
" ])\n",
"\n",
"\n",
"def U_qs_parser(theta, phi, lmd):\n",
" \"\"\"https://github.com/qsharp-community/qsharp-integrations/blob/main/src/OpenQasmReader/Parser.cs#L740-L778\"\"\"\n",
" return simplify(Rx(lmd) * Ry(phi) * Rz(theta))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7f56c103-629a-4080-8cc8-7e0174ff3524",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}\\cos{\\left(\\frac{\\theta}{2} \\right)} & - e^{i \\lambda} \\sin{\\left(\\frac{\\theta}{2} \\right)}\\\\e^{i \\phi} \\sin{\\left(\\frac{\\theta}{2} \\right)} & e^{i \\left(\\lambda + \\phi\\right)} \\cos{\\left(\\frac{\\theta}{2} \\right)}\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ cos(\\theta/2), -exp(I*\\lambda)*sin(\\theta/2)],\n",
"[exp(I*\\phi)*sin(\\theta/2), exp(I*(\\lambda + \\phi))*cos(\\theta/2)]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U_qasm = U_qiskit(theta, phi, lmd)\n",
"U_qasm"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "539fcc6a-d6ab-43b4-b6cc-5e52d39f8c5d",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}0.25 \\left(i \\left(1 - e^{i \\lambda}\\right) \\left(1 - e^{1.0 i \\phi}\\right) + \\left(e^{i \\lambda} + 1\\right) \\left(e^{1.0 i \\phi} + 1\\right)\\right) e^{\\frac{i \\left(- \\lambda - 1.0 \\phi - \\theta\\right)}{2}} & 0.25 \\left(\\left(1 - e^{i \\lambda}\\right) \\left(e^{1.0 i \\phi} + 1\\right) - i \\left(1 - e^{1.0 i \\phi}\\right) \\left(e^{i \\lambda} + 1\\right)\\right) e^{\\frac{i \\left(- \\lambda - 1.0 \\phi + \\theta\\right)}{2}}\\\\0.25 \\left(\\left(1 - e^{i \\lambda}\\right) \\left(e^{1.0 i \\phi} + 1\\right) + i \\left(1 - e^{1.0 i \\phi}\\right) \\left(e^{i \\lambda} + 1\\right)\\right) e^{\\frac{i \\left(- \\lambda - 1.0 \\phi - \\theta\\right)}{2}} & 0.25 \\left(- i \\left(1 - e^{i \\lambda}\\right) \\left(1 - e^{1.0 i \\phi}\\right) + \\left(e^{i \\lambda} + 1\\right) \\left(e^{1.0 i \\phi} + 1\\right)\\right) e^{\\frac{i \\left(- \\lambda - 1.0 \\phi + \\theta\\right)}{2}}\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[0.25*(I*(1 - exp(I*\\lambda))*(1 - exp(1.0*I*\\phi)) + (exp(I*\\lambda) + 1)*(exp(1.0*I*\\phi) + 1))*exp(I*(-\\lambda - 1.0*\\phi - \\theta)/2), 0.25*((1 - exp(I*\\lambda))*(exp(1.0*I*\\phi) + 1) - I*(1 - exp(1.0*I*\\phi))*(exp(I*\\lambda) + 1))*exp(I*(-\\lambda - 1.0*\\phi + \\theta)/2)],\n",
"[0.25*((1 - exp(I*\\lambda))*(exp(1.0*I*\\phi) + 1) + I*(1 - exp(1.0*I*\\phi))*(exp(I*\\lambda) + 1))*exp(I*(-\\lambda - 1.0*\\phi - \\theta)/2), 0.25*(-I*(1 - exp(I*\\lambda))*(1 - exp(1.0*I*\\phi)) + (exp(I*\\lambda) + 1)*(exp(1.0*I*\\phi) + 1))*exp(I*(-\\lambda - 1.0*\\phi + \\theta)/2)]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U_qs = U_qs_parser(theta, phi, lmd)\n",
"U_qs"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "be0b833a-26c9-4b2c-844d-adc834d06665",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U_qs == U_qasm"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3d2b475f-dc30-44f1-a5a0-e86e4a6ad1ab",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}0 & -1\\\\1 & 0\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[0, -1],\n",
"[1, 0]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U_qasm.subs([(theta, pi), (phi, 0), (lmd, 0)])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "75ad10cd-4d3e-46d8-a032-9f1efa1c3c3c",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}- 1.0 i & 0\\\\0 & 1.0 i\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[-1.0*I, 0],\n",
"[ 0, 1.0*I]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U_qs.subs([(theta, pi), (phi, 0), (lmd, 0)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6a8eaf0-8b20-458d-8d46-553ee82f35c1",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment