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

iSWAP gate and Givens gate #304

Merged
merged 6 commits into from
Sep 14, 2023
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
65 changes: 65 additions & 0 deletions src/tequila/circuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,71 @@ def SWAP(first: int, second: int, control: typing.Union[int, list] = None, power
else:
return GeneralizedRotation(angle=power * np.pi, control=control, generator=generator,
eigenvalues_magnitude=0.25)


def iSWAP(first: int, second: int, control: typing.Union[int, list] = None, power: float = 1.0, *args,
**kwargs) -> QCircuit:
"""
Notes
----------
iSWAP gate
.. math::
iSWAP = e^{i\\frac{\\pi}{4} (X \\otimes X + Y \\otimes Y )}

Parameters
----------
first: int
target qubit
second: int
target qubit
control
int or list of ints
power
numeric type (fixed exponent) or hashable type (parametrized exponent)

Returns
-------
QCircuit

"""

generator = paulis.from_string(f"X({first})X({second}) + Y({first})Y({second})")

p0 = paulis.Projector("|00>") + paulis.Projector("|11>")
p0 = p0.map_qubits({0:first, 1:second})

gate = QubitExcitationImpl(angle=power*(-np.pi/2), target=generator.qubits, generator=generator, p0=p0, control=control, compile_options="vanilla", *args, **kwargs)

return QCircuit.wrap_gate(gate)


def Givens(first: int, second: int, control: typing.Union[int, list] = None, angle: float = None, *args,
**kwargs) -> QCircuit:
"""
Notes
----------
Givens gate G
.. math::
G = e^{-i\\theta \\frac{(Y \\otimes X - X \\otimes Y )}{2}}

Parameters
----------
first: int
target qubit
second: int
target qubit
control
int or list of ints
angle
numeric type (fixed exponent) or hashable type (parametrized exponent), theta in the above formula

Returns
-------
QCircuit

"""

return QubitExcitation(target=[second,first], angle=2*angle, control=control, *args, **kwargs) # twice the angle since theta is not divided by two in the matrix exponential


"""
Expand Down
27 changes: 26 additions & 1 deletion tests/test_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tequila import assign_variable, paulis, TequilaWarning
from tequila.circuit._gates_impl import RotationGateImpl
from tequila.circuit.gates import CNOT, ExpPauli, H, Phase, QCircuit, RotationGate, Rx, Ry, Rz, S, \
SWAP, T, Trotterized, u1, u2, u3, X, Y, Z
SWAP, iSWAP, Givens, T, Trotterized, u1, u2, u3, X, Y, Z
from tequila.objective.objective import Variable
from tequila.simulators.simulator_api import simulate
from tequila.wavefunction.qubit_wavefunction import QubitWaveFunction
Expand Down Expand Up @@ -332,6 +332,31 @@ def test_swap():
wfn = simulate(U)
wfnx = simulate(X(2)+X(3))
assert numpy.isclose(numpy.abs(wfn.inner(wfnx))**2,1.0)

def test_iswap():
U = X(0)
U += iSWAP(0, 2, power=0.5)
wfn = simulate(U)
wfnx = simulate(X(2))
assert numpy.isclose(numpy.abs(wfn.inner(wfnx))**2, 0.5)


def test_givens():
U = X(0)
U += Givens(0, 1, angle=-numpy.pi/4)
wfn = simulate(U)
wfnx = simulate(X(0))
assert numpy.isclose(numpy.abs(wfn.inner(wfnx))**2, 0.5)
wfnx = simulate(X(1))
assert numpy.isclose(numpy.abs(wfn.inner(wfnx))**2, 0.5)

U = X(0)
U += Givens(0, 1, angle=numpy.pi/4)
wfn = simulate(U)
wfnx0 = simulate(Phase([0, 1], angle=numpy.pi) + X(0))
wfnx1 = simulate(X(1))
assert numpy.isclose(wfn.inner(wfnx0), -wfn.inner(wfnx1))


def test_variable_map():
U = Ry(angle="a", target=0) + Rx(angle="b", target=1) + Rz(angle="c", target=2) + H(angle="d", target=3) + ExpPauli(paulistring="X(0)Y(1)Z(2)", angle="e")
Expand Down
Loading