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