Skip to content
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
5 changes: 3 additions & 2 deletions projectq/backends/_resource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from projectq.cengines import DummyEngine, MainEngine, NotYetMeasuredError
from projectq.meta import LogicalQubitIDTag
from projectq.ops import All, Allocate, CNOT, Command, H, Measure, QFT, Rz, X
from projectq.ops import All, Allocate, CNOT, Command, H, Measure, QFT, Rz, Rzz, X
from projectq.types import WeakQubitRef

from projectq.backends import ResourceCounter
Expand Down Expand Up @@ -74,14 +74,15 @@ def test_resource_counter():
CNOT | (qubit1, qubit3)
Rz(0.1) | qubit1
Rz(0.3) | qubit1
Rzz(0.5) | qubit1

All(Measure) | qubit1 + qubit3

with pytest.raises(NotYetMeasuredError):
int(qubit1)

assert resource_counter.max_width == 2
assert resource_counter.depth_of_dag == 5
assert resource_counter.depth_of_dag == 6

str_repr = str(resource_counter)
assert str_repr.count(" HGate : 1") == 1
Expand Down
48 changes: 45 additions & 3 deletions projectq/ops/_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@
Contains definitions of standard gates such as
* Hadamard (H)
* Pauli-X (X / NOT)
* Pauli-Y (Y)
* Pauli-Z (Z)
* S and its inverse (S / Sdagger)
* T and its inverse (T / Tdagger)
* SqrtX gate (SqrtX)
* Swap gate (Swap)
* SqrtSwap gate (SqrtSwap)
* Entangle (Entangle)
* Phase gate (Ph)
* Rotation-X (Rx)
* Rotation-Y (Ry)
* Rotation-Z (Rz)
* Rotation-XX on two qubits (Rxx)
* Rotation-YY on two qubits (Ryy)
* Rotation-ZZ on two qubits (Rzz)
* Phase-shift (R)
* Measurement (Measure)

and meta gates, i.e.,
* Allocate / Deallocate qubits
* Flush gate (end of circuit)
* Barrier
* FlipBits
"""

import math
Expand Down Expand Up @@ -110,7 +122,7 @@ def __str__(self):

#: Shortcut (instance of) :class:`projectq.ops.SGate`
S = SGate()
#: Shortcut (instance of) :class:`projectq.ops.SGate`
#: Inverse (and shortcut) of :class:`projectq.ops.SGate`
Sdag = Sdagger = get_inverse(S)


Expand All @@ -125,7 +137,7 @@ def __str__(self):

#: Shortcut (instance of) :class:`projectq.ops.TGate`
T = TGate()
#: Shortcut (instance of) :class:`projectq.ops.TGate`
#: Inverse (and shortcut) of :class:`projectq.ops.TGate`
Tdag = Tdagger = get_inverse(T)


Expand Down Expand Up @@ -217,7 +229,7 @@ def matrix(self):


class Ry(BasicRotationGate):
""" RotationX gate class """
""" RotationY gate class """
@property
def matrix(self):
return np.matrix([[math.cos(0.5 * self.angle),
Expand All @@ -234,6 +246,36 @@ def matrix(self):
[0, cmath.exp(.5 * 1j * self.angle)]])


class Rxx(BasicRotationGate):
""" RotationXX gate class """
@property
def matrix(self):
return np.matrix([[cmath.cos(.5 * self.angle), 0, 0, -1j*cmath.sin(.5 * self.angle)],
[0, cmath.cos( .5 * self.angle), -1j*cmath.sin(.5 * self.angle), 0],
[0, -1j*cmath.sin(.5 * self.angle), cmath.cos( .5 * self.angle), 0],
[-1j*cmath.sin(.5 * self.angle), 0, 0, cmath.cos( .5 * self.angle)]])


class Ryy(BasicRotationGate):
""" RotationYY gate class """
@property
def matrix(self):
return np.matrix([[cmath.cos(.5 * self.angle), 0, 0, 1j*cmath.sin(.5 * self.angle)],
[0, cmath.cos( .5 * self.angle), -1j*cmath.sin(.5 * self.angle), 0],
[0, -1j*cmath.sin(.5 * self.angle), cmath.cos( .5 * self.angle), 0],
[1j*cmath.sin(.5 * self.angle), 0, 0, cmath.cos( .5 * self.angle)]])


class Rzz(BasicRotationGate):
""" RotationZZ gate class """
@property
def matrix(self):
return np.matrix([[cmath.exp(-.5 * 1j * self.angle), 0, 0, 0],
[0, cmath.exp( .5 * 1j * self.angle), 0, 0],
[0, 0, cmath.exp( .5 * 1j * self.angle), 0],
[0, 0, 0, cmath.exp(-.5 * 1j * self.angle)]])


class R(BasicPhaseGate):
""" Phase-shift gate (equivalent to Rz up to a global phase) """
@property
Expand Down
36 changes: 36 additions & 0 deletions projectq/ops/_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,42 @@ def test_rz(angle):
assert np.allclose(gate.matrix, expected_matrix)


@pytest.mark.parametrize("angle", [0, 0.2, 2.1, 4.1, 2 * math.pi,
4 * math.pi])
def test_rxx(angle):
gate = _gates.Rxx(angle)
expected_matrix = np.matrix([[cmath.cos(.5 * angle), 0, 0, -1j * cmath.sin(.5 * angle)],
[0, cmath.cos(.5 * angle), -1j * cmath.sin(.5 * angle), 0],
[0, -1j * cmath.sin(.5 * angle), cmath.cos(.5 * angle), 0],
[-1j * cmath.sin(.5 * angle), 0, 0, cmath.cos(.5 * angle)]])
assert gate.matrix.shape == expected_matrix.shape
assert np.allclose(gate.matrix, expected_matrix)


@pytest.mark.parametrize("angle", [0, 0.2, 2.1, 4.1, 2 * math.pi,
4 * math.pi])
def test_ryy(angle):
gate = _gates.Ryy(angle)
expected_matrix = np.matrix([[cmath.cos(.5 * angle), 0, 0, 1j * cmath.sin(.5 * angle)],
[0, cmath.cos(.5 * angle), -1j * cmath.sin(.5 * angle), 0],
[0, -1j * cmath.sin(.5 * angle), cmath.cos(.5 * angle), 0],
[ 1j * cmath.sin(.5 * angle), 0, 0, cmath.cos(.5 * angle)]])
assert gate.matrix.shape == expected_matrix.shape
assert np.allclose(gate.matrix, expected_matrix)


@pytest.mark.parametrize("angle", [0, 0.2, 2.1, 4.1, 2 * math.pi,
4 * math.pi])
def test_rzz(angle):
gate = _gates.Rzz(angle)
expected_matrix = np.matrix([[cmath.exp(-.5 * 1j * angle), 0, 0, 0],
[0, cmath.exp( .5 * 1j * angle), 0, 0],
[0, 0, cmath.exp( .5 * 1j * angle), 0],
[0, 0, 0, cmath.exp(-.5 * 1j * angle)]])
assert gate.matrix.shape == expected_matrix.shape
assert np.allclose(gate.matrix, expected_matrix)


@pytest.mark.parametrize("angle", [0, 0.2, 2.1, 4.1, 2 * math.pi])
def test_ph(angle):
gate = _gates.Ph(angle)
Expand Down