Skip to content

[CQT-293] Remove generators #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 6, 2025
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* **Removed** for now removed features.


## [0.3.2] - [ xxxx-yy-zz ]

### Changed

- Refactor: removed generators


## [0.3.1] - [ 2025-01-31 ]

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ _Output_:

qubit[1] q

Anonymous gate: BlochSphereRotation(Qubit[0], axis=[1. 0. 0.], angle=3.14159, phase=0.0)
BlochSphereRotation(qubit=Qubit[0], axis=[1. 0. 0.], angle=3.14159, phase=0.0)

In the above example, OpenSquirrel has merged all the Rx gates together.
Yet, for now, OpenSquirrel does not recognize that this results in a single Rx
Expand Down Expand Up @@ -435,9 +435,9 @@ print(ZYZDecomposer().decompose(H(0)))
```
_Output_:

[BlochSphereRotation(Qubit[0], axis=Axis[0. 0. 1.], angle=1.5707963267948966, phase=0.0),
BlochSphereRotation(Qubit[0], axis=Axis[0. 1. 0.], angle=1.5707963267948966, phase=0.0),
BlochSphereRotation(Qubit[0], axis=Axis[0. 0. 1.], angle=1.5707963267948966, phase=0.0)]
[BlochSphereRotation(qubit=Qubit[0], axis=Axis[0. 0. 1.], angle=1.5707963267948966, phase=0.0),
BlochSphereRotation(qubit=Qubit[0], axis=Axis[0. 1. 0.], angle=1.5707963267948966, phase=0.0),
BlochSphereRotation(qubit=Qubit[0], axis=Axis[0. 0. 1.], angle=1.5707963267948966, phase=0.0)]


## Exporting a circuit
Expand Down
122 changes: 84 additions & 38 deletions example/decompositions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"\n",
"The ABA decomposition (or KAK decomposition) is a decomposition method that uses three consecutive Pauli rotation gates to form any unitary gate. Thus, any arbitrary single qubit gate can be expressed as a product of rotations: a rotation around the A axis, followed by a rotation around the B axis, and then another rotation around the A axis. The mathematical formalism for this rotation can be seen in section 3.1.\n",
"\n",
"In OpenSquirrel, this can be found in the `get_decomposition_angles` method from the `opensquirrel.decomposer.aba_decomposer` module. The ABA Decomposition can be applied to list of gates or a circuit. Below we will first focus on decomposing a circuit.\n",
"In OpenSquirrel, this can be found in the `get_decomposition_angles` method from the `opensquirrel.passes.decomposer.aba_decomposer` module. The ABA Decomposition can be applied to list of gates or a circuit. Below we will first focus on decomposing a circuit.\n",
"\n",
"The circuit chosen is a single qubit circuit with an added Hadamard, Z, Y, and Rx gate. \n",
"\n",
Expand Down Expand Up @@ -62,15 +62,14 @@
"source": [
"import math\n",
"\n",
"from opensquirrel import CircuitBuilder\n",
"from opensquirrel.ir import Float, Qubit\n",
"from opensquirrel.circuit_builder import CircuitBuilder\n",
"\n",
"# Build the circuit structure using the CircuitBuilder\n",
"builder = CircuitBuilder(qubit_register_size=1)\n",
"builder.H(Qubit(0))\n",
"builder.Z(Qubit(0))\n",
"builder.Y(Qubit(0))\n",
"builder.Rx(Qubit(0), Float(math.pi / 3))\n",
"builder.H(0)\n",
"builder.Z(0)\n",
"builder.Y(0)\n",
"builder.Rx(0, math.pi / 3)\n",
"\n",
"# Create a new circuit from the constructed structure\n",
"circuit = builder.to_circuit()\n",
Expand All @@ -81,7 +80,9 @@
"cell_type": "markdown",
"id": "afe111839ab98b22",
"metadata": {},
"source": "Above we can see the current gates in our circuit. Having created a circuit, we can now use an ABA decomposition in `opensquirrel.decomposer.aba_decomposer` to decompose the gates in the circuit. For this example, we will apply the Z-Y-Z decomposition using the `ZYZDecomposer`. "
"source": [
"Above we can see the current gates in our circuit. Having created a circuit, we can now use an ABA decomposition in `opensquirrel.passes.decomposer.aba_decomposer` to decompose the gates in the circuit. For this example, we will apply the Z-Y-Z decomposition using the `ZYZDecomposer`. "
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -116,7 +117,7 @@
}
],
"source": [
"from opensquirrel.passes.decomposer import ZYZDecomposer\n",
"from opensquirrel.passes.decomposer.aba_decomposer import ZYZDecomposer\n",
"\n",
"# Decompose the circuit using ZYZDecomposer\n",
"circuit.decompose(decomposer=ZYZDecomposer())\n",
Expand Down Expand Up @@ -170,9 +171,9 @@
],
"source": [
"from opensquirrel import H\n",
"from opensquirrel.passes.decomposer import XZXDecomposer\n",
"from opensquirrel.passes.decomposer.aba_decomposer import XZXDecomposer\n",
"\n",
"XZXDecomposer().decompose(H(Qubit(0)))"
"XZXDecomposer().decompose(H(0))"
]
},
{
Expand Down Expand Up @@ -226,10 +227,10 @@
"source": [
"# Build the circuit structure using the CircuitBuilder\n",
"builder = CircuitBuilder(qubit_register_size=1)\n",
"builder.H(Qubit(0))\n",
"builder.Z(Qubit(0))\n",
"builder.X(Qubit(0))\n",
"builder.Rx(Qubit(0), Float(math.pi / 3))\n",
"builder.H(0)\n",
"builder.Z(0)\n",
"builder.X(0)\n",
"builder.Rx(0, math.pi / 3)\n",
"\n",
"# Create a new circuit from the constructed structure\n",
"circuit = builder.to_circuit()\n",
Expand All @@ -240,7 +241,9 @@
"cell_type": "markdown",
"id": "69517d8287c1777d",
"metadata": {},
"source": "The `McKayDecomposer` is called from `opensquirrel.decomposer.mckay_decomposer` and used in a similar method to the `ABADecomposer`."
"source": [
"The `McKayDecomposer` is called from `opensquirrel.passes.decomposer` and used in a similar method to the `ABADecomposer`."
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -358,9 +361,9 @@
"source": [
"# Build the circuit structure using the CircuitBuilder\n",
"builder = CircuitBuilder(qubit_register_size=2)\n",
"builder.CZ(Qubit(0), Qubit(1))\n",
"builder.CR(Qubit(0), Qubit(1), Float(math.pi / 3))\n",
"builder.CR(Qubit(1), Qubit(0), Float(math.pi / 2))\n",
"builder.CZ(0, 1)\n",
"builder.CR(0, 1, math.pi / 3)\n",
"builder.CR(1, 0, math.pi / 2)\n",
"\n",
"# Create a new circuit from the constructed structure\n",
"circuit = builder.to_circuit()\n",
Expand All @@ -371,7 +374,9 @@
"cell_type": "markdown",
"id": "8c945ab4572e9f77",
"metadata": {},
"source": "We can import `CNOTDecomposer` from `opensquirrel.decomposer.cnot_decomposer`. The above circuit can then be decomposed using `CNOTDecomposer` as seen below."
"source": [
"We can import `CNOTDecomposer` from `opensquirrel.passes.decomposer`. The above circuit can then be decomposed using `CNOTDecomposer` as seen below."
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -475,7 +480,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + - \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$",
"text/latex": [
"$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + - \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$"
],
"text/plain": [
"cos(theta_2/2)*cos((theta_1 + theta_3)/2) + (-sin(theta_2/2)*sin((theta_1 - theta_3)/2))*i + sin(theta_2/2)*cos((theta_1 - theta_3)/2)*j + sin((theta_1 + theta_3)/2)*cos(theta_2/2)*k"
]
Expand All @@ -502,7 +509,9 @@
"cell_type": "markdown",
"id": "6e776f2cb7cae465",
"metadata": {},
"source": "Let us change variables and define $p\\equiv\\theta_1 + \\theta_3$ and $m\\equiv\\theta_1 - \\theta_3$."
"source": [
"Let us change variables and define $p\\equiv\\theta_1 + \\theta_3$ and $m\\equiv\\theta_1 - \\theta_3$."
]
},
{
"cell_type": "code",
Expand All @@ -517,7 +526,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} + - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} j + \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$",
"text/latex": [
"$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} + - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} j + \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} k$"
],
"text/plain": [
"cos(p/2)*cos(theta_2/2) + (-sin(m/2)*sin(theta_2/2))*i + sin(theta_2/2)*cos(m/2)*j + sin(p/2)*cos(theta_2/2)*k"
]
Expand All @@ -539,7 +550,9 @@
"cell_type": "markdown",
"id": "1ece3d36eb99512b",
"metadata": {},
"source": "The original rotation's quaternion $q$ can be defined in Sympy accordingly:"
"source": [
"The original rotation's quaternion $q$ can be defined in Sympy accordingly:"
]
},
{
"cell_type": "code",
Expand All @@ -554,7 +567,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\alpha}{2} \\right)} + n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)} i + n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)} j + n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)} k$",
"text/latex": [
"$\\displaystyle \\cos{\\left(\\frac{\\alpha}{2} \\right)} + n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)} i + n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)} j + n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)} k$"
],
"text/plain": [
"cos(alpha/2) + n_x*sin(alpha/2)*i + n_y*sin(alpha/2)*j + n_z*sin(alpha/2)*k"
]
Expand All @@ -577,7 +592,9 @@
"cell_type": "markdown",
"id": "7cb50842c8fa111a",
"metadata": {},
"source": "We get the following system of equations, where the unknowns are $p$, $m$, and $\\theta_2$:\n"
"source": [
"We get the following system of equations, where the unknowns are $p$, $m$, and $\\theta_2$:\n"
]
},
{
"cell_type": "code",
Expand All @@ -592,7 +609,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$",
"text/latex": [
"$\\displaystyle \\cos{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$"
],
"text/plain": [
"Eq(cos(p/2)*cos(theta_2/2), cos(alpha/2))"
]
Expand All @@ -602,7 +621,9 @@
},
{
"data": {
"text/latex": "$\\displaystyle - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$",
"text/latex": [
"$\\displaystyle - \\sin{\\left(\\frac{m}{2} \\right)} \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{x} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$"
],
"text/plain": [
"Eq(-sin(m/2)*sin(theta_2/2), n_x*sin(alpha/2))"
]
Expand All @@ -612,7 +633,9 @@
},
{
"data": {
"text/latex": "$\\displaystyle \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} = n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$",
"text/latex": [
"$\\displaystyle \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{m}{2} \\right)} = n_{y} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$"
],
"text/plain": [
"Eq(sin(theta_2/2)*cos(m/2), n_y*sin(alpha/2))"
]
Expand All @@ -622,7 +645,9 @@
},
{
"data": {
"text/latex": "$\\displaystyle \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$",
"text/latex": [
"$\\displaystyle \\sin{\\left(\\frac{p}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} = n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}$"
],
"text/plain": [
"Eq(sin(p/2)*cos(theta_2/2), n_z*sin(alpha/2))"
]
Expand All @@ -646,7 +671,9 @@
"cell_type": "markdown",
"id": "e6b34ef9cb46f2e4",
"metadata": {},
"source": "Instead, assume $\\sin(p/2) \\neq 0$, then we can substitute in the first equation $\\cos\\left(\\theta_2/2\\right)$ with its value computed from the last equation. We get:"
"source": [
"Instead, assume $\\sin(p/2) \\neq 0$, then we can substitute in the first equation $\\cos\\left(\\theta_2/2\\right)$ with its value computed from the last equation. We get:"
]
},
{
"cell_type": "code",
Expand All @@ -661,7 +688,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\frac{n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}}{\\tan{\\left(\\frac{p}{2} \\right)}} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$",
"text/latex": [
"$\\displaystyle \\frac{n_{z} \\sin{\\left(\\frac{\\alpha}{2} \\right)}}{\\tan{\\left(\\frac{p}{2} \\right)}} = \\cos{\\left(\\frac{\\alpha}{2} \\right)}$"
],
"text/plain": [
"Eq(n_z*sin(alpha/2)/tan(p/2), cos(alpha/2))"
]
Expand Down Expand Up @@ -718,7 +747,9 @@
"cell_type": "markdown",
"id": "e3fc4cdb6d15dd1",
"metadata": {},
"source": "The terms are similar to the other decompositions, XZX, YXY, ZXZ, XYX and YZY. However, for ZXZ, XYX and YZY, the $i$ term in the quaternion is positive as seen below in the YZY decomposition."
"source": [
"The terms are similar to the other decompositions, XZX, YXY, ZXZ, XYX and YZY. However, for ZXZ, XYX and YZY, the $i$ term in the quaternion is positive as seen below in the YZY decomposition."
]
},
{
"cell_type": "code",
Expand All @@ -733,7 +764,9 @@
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} k$",
"text/latex": [
"$\\displaystyle \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\sin{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} i + \\sin{\\left(\\frac{\\theta_{1} + \\theta_{3}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{2}}{2} \\right)} j + \\sin{\\left(\\frac{\\theta_{2}}{2} \\right)} \\cos{\\left(\\frac{\\theta_{1} - \\theta_{3}}{2} \\right)} k$"
],
"text/plain": [
"cos(theta_2/2)*cos((theta_1 + theta_3)/2) + sin(theta_2/2)*sin((theta_1 - theta_3)/2)*i + sin((theta_1 + theta_3)/2)*cos(theta_2/2)*j + sin(theta_2/2)*cos((theta_1 - theta_3)/2)*k"
]
Expand All @@ -758,7 +791,9 @@
"cell_type": "markdown",
"id": "a41b84ed5f3365b2",
"metadata": {},
"source": "Thus, in order to correct for the orientation of the rotation, $\\theta_1$ and $\\theta_3$ are swapped. "
"source": [
"Thus, in order to correct for the orientation of the rotation, $\\theta_1$ and $\\theta_3$ are swapped. "
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -793,6 +828,8 @@
},
{
"cell_type": "markdown",
"id": "866e89a24587f0af",
"metadata": {},
"source": [
"### 3.3 ABA Decomposition\n",
"In a two qubit system, the unitary matrix of a controlled operation is seen below,\n",
Expand Down Expand Up @@ -844,11 +881,20 @@
"C &= Rz\\bigg(\\frac{1}{2} \\theta_0 - \\frac{1}{2} \\theta_2 \\bigg)\n",
"\\end{split}\n",
"$$"
],
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "3cc1e9963f3e794d",
"metadata": {
"collapsed": false
"ExecuteTime": {
"end_time": "2024-08-05T11:35:27.882752Z",
"start_time": "2024-08-05T11:35:27.871794Z"
}
},
"id": "c084046b0bb17ec9"
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading