Skip to content
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

add get_inverse features #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.hypothesis/
.venv/
.vscode/
.idea/
.mypy_cache/
.DS_Store
docs/source/autogen
Expand Down
18 changes: 18 additions & 0 deletions bqskit/ir/gates/constant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bqskit.ir.gates.constant.clock import ClockGate
from bqskit.ir.gates.constant.cpi import CPIGate
from bqskit.ir.gates.constant.cs import CSGate
from bqskit.ir.gates.constant.csdg import CSDGGate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep convention and have the dg be lower case. This applies everywhere and to all new gates.

from bqskit.ir.gates.constant.csum import CSUMGate
from bqskit.ir.gates.constant.ct import CTGate
from bqskit.ir.gates.constant.cx import CNOTGate
Expand All @@ -17,6 +18,7 @@
from bqskit.ir.gates.constant.h import HGate
from bqskit.ir.gates.constant.identity import IdentityGate
from bqskit.ir.gates.constant.iswap import ISwapGate
from bqskit.ir.gates.constant.iswapdg import ISwapDGGate
from bqskit.ir.gates.constant.itoffoli import IToffoliGate
from bqskit.ir.gates.constant.pd import PDGate
from bqskit.ir.gates.constant.permutation import PermutationGate
Expand All @@ -27,21 +29,28 @@
from bqskit.ir.gates.constant.sdg import SdgGate
from bqskit.ir.gates.constant.shift import ShiftGate
from bqskit.ir.gates.constant.sqrtcnot import SqrtCNOTGate
from bqskit.ir.gates.constant.sqrtcnotdg import SqrtCNOTDGGate
from bqskit.ir.gates.constant.sqrtiswap import SqrtISwapGate
from bqskit.ir.gates.constant.sqrtiswapdg import SqrtISwapDGGate
from bqskit.ir.gates.constant.subswap import SubSwapGate
from bqskit.ir.gates.constant.swap import SwapGate
from bqskit.ir.gates.constant.sx import SqrtXGate
from bqskit.ir.gates.constant.sx import SXGate
from bqskit.ir.gates.constant.sxdg import SqrtXDGGate
from bqskit.ir.gates.constant.sxdg import SXDGGate
from bqskit.ir.gates.constant.sycamore import SycamoreGate
from bqskit.ir.gates.constant.t import TGate
from bqskit.ir.gates.constant.tdg import TdgGate
from bqskit.ir.gates.constant.unitary import ConstantUnitaryGate
from bqskit.ir.gates.constant.x import XGate
from bqskit.ir.gates.constant.xx import XXGate
from bqskit.ir.gates.constant.xxdg import XXDGGate
from bqskit.ir.gates.constant.y import YGate
from bqskit.ir.gates.constant.yy import YYGate
from bqskit.ir.gates.constant.yydg import YYDGGate
from bqskit.ir.gates.constant.z import ZGate
from bqskit.ir.gates.constant.zz import ZZGate
from bqskit.ir.gates.constant.zzdg import ZZDGGate

__all__ = [
'BGate',
Expand All @@ -51,6 +60,7 @@
'ClockGate',
'CPIGate',
'CSGate',
'CSDGGate',
'CSUMGate',
'CTGate',
'CNOTGate',
Expand All @@ -60,6 +70,7 @@
'HGate',
'IdentityGate',
'ISwapGate',
'ISwapDGGate',
'IToffoliGate',
'PDGate',
'PermutationGate',
Expand All @@ -70,19 +81,26 @@
'SdgGate',
'ShiftGate',
'SqrtCNOTGate',
'SqrtCNOTDGGate',
'SqrtISwapGate',
'SqrtISwapDGGate',
'SubSwapGate',
'SwapGate',
'SqrtXGate',
'SXGate',
'SqrtXDGGate',
'SXDGGate',
'SycamoreGate',
'TGate',
'TdgGate',
'ConstantUnitaryGate',
'XGate',
'XXGate',
'XXDGGate',
'YGate',
'YYGate',
'YYDGGate',
'ZGate',
'ZZGate',
'ZZDGGate',
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to add the new gates to the autodoc in bqskit.ir.gates.__init__.py

If you can automate that, that would be awesome!

19 changes: 19 additions & 0 deletions bqskit/ir/gates/constant/cs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module implements the CSGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand Down Expand Up @@ -32,3 +33,21 @@ class CSGate(ConstantGate, QubitGate):
[0, 0, 0, 1j],
],
)

def get_qasm_gate_def(self) -> str:
"""Returns a qasm gate definition block for this gate."""
return (
'gate cs a,b\n'
'{\n'
'\tp(pi/4) a;\n'
'\tcx a, b;\n'
'\tp(-pi/4) b;\n'
'\tcx a, b;\n'
'\tp(pi/4) b;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.csdg import CSDGGate
return CSDGGate()
53 changes: 53 additions & 0 deletions bqskit/ir/gates/constant/csdg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""This module implements the CSDGGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import should be under a TYPE_CHECKING clause everywhere unless necessary for code execution

from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix


class CSDGGate(ConstantGate, QubitGate):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the CTGate?

"""
The Controlled-S Dagger gate.

The CS Dagger gate is given by the following unitary:

.. math::

\\begin{pmatrix}
1 & 0 & 0 & 0 \\\\
0 & 1 & 0 & 0 \\\\
0 & 0 & 1 & 0 \\\\
0 & 0 & 0 & -i \\\\
\\end{pmatrix}
"""

_num_qudits = 2
_qasm_name = 'csdg'
_utry = UnitaryMatrix(
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, -1j],
],
)

def get_qasm_gate_def(self) -> str:
"""Returns a qasm gate definition block for this gate."""
return (
'gate csdg a,b\n'
'{\n'
'\tp(-pi/4) a;\n'
'\tcx a, b;\n'
'\tp(pi/4) b;\n'
'\tcx a, b;\n'
'\tp(-pi/4) b;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.cs import CSGate
return CSGate()
6 changes: 6 additions & 0 deletions bqskit/ir/gates/constant/iswap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module implements the ISwapGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand Down Expand Up @@ -46,3 +47,8 @@ def get_qasm_gate_def(self) -> str:
'\th b;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.iswapdg import ISwapDGGate
return ISwapDGGate()
55 changes: 55 additions & 0 deletions bqskit/ir/gates/constant/iswapdg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""This module implements the ISwapDGGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix


class ISwapDGGate(ConstantGate, QubitGate):
"""
The two qubit swap and phase iSWAP Dagger gate.

The ISwap Dagger gate is given by the following unitary:

.. math::

\\begin{pmatrix}
1 & 0 & 0 & 0 \\\\
0 & 0 & -i & 0 \\\\
0 & -i & 0 & 0 \\\\
0 & 0 & 0 & 1 \\\\
\\end{pmatrix}
"""

_num_qudits = 2
_qasm_name = 'iswap_dg'
_utry = UnitaryMatrix(
[
[1, 0, 0, 0],
[0, 0, -1j, 0],
[0, -1j, 0, 0],
[0, 0, 0, 1],
],
)

def get_qasm_gate_def(self) -> str:
"""Returns a qasm gate definition block for this gate."""
"""iswap_dg q0,q1 { h q1; cx q1,q0; cx q0,q1; h q0; sdg q1; sdg q0; }"""
return (
'gate iswap_dg a,b\n'
'{\n'
'\th b;\n'
'\tcx b, a;\n'
'\tcx a, b;\n'
'\th a;\n'
'\tsdg b;\n'
'\tsdg a;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.iswap import ISwapGate
return ISwapGate()
6 changes: 6 additions & 0 deletions bqskit/ir/gates/constant/s.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module implements the SGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand All @@ -26,3 +27,8 @@ class SGate(ConstantGate, QubitGate):
[0, 1j],
],
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.sdg import SdgGate
return SdgGate()
6 changes: 6 additions & 0 deletions bqskit/ir/gates/constant/sdg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module implements the SdgGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand All @@ -26,3 +27,8 @@ class SdgGate(ConstantGate, QubitGate):
[0, -1j],
],
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.s import SGate
return SGate()
6 changes: 6 additions & 0 deletions bqskit/ir/gates/constant/sqrtcnot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module implements the SqrtCNOTGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand Down Expand Up @@ -32,3 +33,8 @@ class SqrtCNOTGate(ConstantGate, QubitGate):
[0, 0, 0.5 - 0.5j, 0.5 + 0.5j],
],
)

def get_inverse(self) -> Gate:
""""Return the inverse of this gate."""
from bqskit.ir.gates.constant.sqrtcnotdg import SqrtCNOTDGGate
return SqrtCNOTDGGate()
61 changes: 61 additions & 0 deletions bqskit/ir/gates/constant/sqrtcnotdg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""This module implements the SqrtCNOTDGGate."""
from __future__ import annotations

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix


class SqrtCNOTDGGate(ConstantGate, QubitGate):
"""
The Square root Controlled-X gate.

The SqrtCNOT Dagger gate is given by the following unitary:

.. math::

\\begin{pmatrix}
1 & 0 & 0 & 0 \\\\
0 & 1 & 0 & 0 \\\\
0 & 0 & \\frac{1}{2} - \\frac{1}{2}i & \\frac{1}{2} + \\frac{1}{2}i \\\\
0 & 0 & \\frac{1}{2} + \\frac{1}{2}i & \\frac{1}{2} - \\frac{1}{2}i \\\\
\\end{pmatrix}
"""

_num_qudits = 2
_qasm_name = 'csxdg'
_utry = UnitaryMatrix(
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0.5 - 0.5j, 0.5 + 0.5j],
[0, 0, 0.5 + 0.5j, 0.5 - 0.5j],
],
)

def get_qasm_gate_def(self) -> str:
"""Returns a qasm gate definition block for this gate."""
"""Gate csxdg q0,q1 { u(0,0,pi/4) q1; cx q0,q1; u(0,0,-pi/4) q1; cx
q0,q1; cu(pi/2,0,pi,0) q0,q1; u(0,0,pi/4) q1; cx q0,q1; u(0,0,-pi/4) q1;
cx q0,q1; p(pi/4) q0; }"""
return (
'gate csxdg a,b\n'
'{\n'
'\tu(0,0,pi/4) b;\n'
'\tcx a,b;\n'
'\tu(0,0,-pi/4) b;\n'
'\tcx a,b;\n'
'\tcu(pi/2,0,pi,0) a,b;\n'
'\tu(0,0,pi/4) b;\n'
'\tcx a,b;\n'
'\tu(0,0,-pi/4) b;\n'
'\tcx a,b;\n'
'\tp(pi/4) a;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.sqrtcnot import SqrtCNOTGate
return SqrtCNOTGate()
22 changes: 22 additions & 0 deletions bqskit/ir/gates/constant/sqrtiswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import math

from bqskit.ir.gate import Gate
from bqskit.ir.gates.constantgate import ConstantGate
from bqskit.ir.gates.qubitgate import QubitGate
from bqskit.qis.unitary.unitarymatrix import UnitaryMatrix
Expand Down Expand Up @@ -34,3 +35,24 @@ class SqrtISwapGate(ConstantGate, QubitGate):
[0, 0, 0, 1],
],
)

def get_qasm_gate_def(self) -> str:
"""Returns a qasm gate definition block for this gate."""
return (
'gate sqisw a,b\n'
'{\n'
'\tcx a,b;\n'
'\th a;\n'
'\tcx b,a;\n'
'\tt a;\n'
'\tcx b,a;\n'
'\ttdg a;\n'
'\th a;\n'
'\tcx a,b;\n'
'}\n'
)

def get_inverse(self) -> Gate:
"""Return the inverse of this gate."""
from bqskit.ir.gates.constant.sqrtiswapdg import SqrtISwapDGGate
return SqrtISwapDGGate()
Loading