Skip to content

Commit 5aab56b

Browse files
Addition of test file for the rz2rx decomposition
Addition of test file for the rz2rx decomposition and edit to comments in the rz2rx file
1 parent a37c182 commit 5aab56b

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\daisy\\Anaconda3\\envs\\py36\\python.exe"
3+
}

projectq/setups/decompositions/rz2rx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
"""
16-
Registers a decomposition for the Ry gate into an Rz and Rx(pi/2) gate.
16+
Registers a decomposition for the Rz gate into an Rx and Ry(pi/2) or Ry(-pi/2) gate.
1717
"""
1818

1919
import math
@@ -24,7 +24,7 @@
2424

2525

2626
def _decompose_rz_P(cmd):
27-
""" Decompose the Ry gate."""
27+
""" Decompose the Rz using negative angle."""
2828
qubit = cmd.qubits[0]
2929
eng = cmd.engine
3030
angle = cmd.gate.angle
@@ -36,7 +36,7 @@ def _decompose_rz_P(cmd):
3636
Uncompute(eng)
3737

3838
def _decompose_rz_M(cmd):
39-
""" Decompose the Ry gate."""
39+
""" Decompose the Rz using positive angle."""
4040
qubit = cmd.qubits[0]
4141
eng = cmd.engine
4242
angle = cmd.gate.angle
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
"Tests for projectq.setups.decompositions.rx2rz.py"
3+
4+
import math
5+
6+
import pytest
7+
8+
from projectq import MainEngine
9+
from projectq.backends import Simulator
10+
from projectq.cengines import (AutoReplacer, DecompositionRuleSet,
11+
DummyEngine, InstructionFilter, MainEngine)
12+
from projectq.meta import Control
13+
from projectq.ops import Measure, Ph, Rz
14+
15+
# from . import rz2rx
16+
import rz2rx
17+
18+
import sys
19+
20+
sys.path.insert(0, 'C:\\Users\\daisy\\OneDrive\\Documents\\Sussex masters\\Masters project\\projectQ_practice_branch\\ProjectQ')
21+
22+
def test_recognize_correct_gates():
23+
""" Check that Rz gate is not part of a two-qubit control
24+
gate """
25+
# Creates a circuit and checks that there is only
26+
# a ctrl qubit if you create one in the circuit
27+
saving_backend = DummyEngine(save_commands=True)
28+
eng = MainEngine(backend=saving_backend)
29+
qubit = eng.allocate_qubit()
30+
ctrl_qubit = eng.allocate_qubit()
31+
eng.flush()
32+
Rz(0.3) | qubit
33+
with Control(eng, ctrl_qubit):
34+
Rz(0.4) | qubit
35+
eng.flush(deallocate_qubits=True)
36+
assert rz2rx._recognize_RzNoCtrl(saving_backend.received_commands[3])
37+
assert not rz2rx._recognize_RzNoCtrl(saving_backend.received_commands[4])
38+
39+
40+
def rz_decomp_gates(eng, cmd):
41+
""" Check that cmd.gate is the gate Rz """
42+
g = cmd.gate
43+
if isinstance(g, Rz):
44+
return False
45+
else:
46+
return True
47+
48+
49+
@pytest.mark.parametrize("angle", [0, math.pi, 2*math.pi, 4*math.pi, 0.5])
50+
def test_decomposition(angle):
51+
""" Test whether the decomposition of Rz results in
52+
the same superposition of |0> and |1> as just using Rz """
53+
for basis_state in ([1, 0], [0, 1]):
54+
correct_dummy_eng = DummyEngine(save_commands=True)
55+
correct_eng = MainEngine(backend=Simulator(),
56+
engine_list=[correct_dummy_eng])
57+
58+
rule_set = DecompositionRuleSet(modules=[rz2rx])
59+
test_dummy_eng = DummyEngine(save_commands=True)
60+
test_eng = MainEngine(backend=Simulator(),
61+
engine_list=[AutoReplacer(rule_set),
62+
InstructionFilter(rz_decomp_gates),
63+
test_dummy_eng])
64+
65+
correct_qb = correct_eng.allocate_qubit()
66+
Rz(angle) | correct_qb
67+
correct_eng.flush()
68+
69+
test_qb = test_eng.allocate_qubit()
70+
Rz(angle) | test_qb
71+
test_eng.flush()
72+
73+
assert correct_dummy_eng.received_commands[1].gate == Rz(angle)
74+
assert test_dummy_eng.received_commands[1].gate != Rz(angle)
75+
76+
for fstate in ['0', '1']:
77+
test = test_eng.backend.get_amplitude(fstate, test_qb)
78+
correct = correct_eng.backend.get_amplitude(fstate, correct_qb)
79+
assert correct == pytest.approx(test, rel=1e-12, abs=1e-12)
80+
81+
Measure | test_qb
82+
Measure | correct_qb

0 commit comments

Comments
 (0)