forked from ProjectQ-Framework/ProjectQ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of test file for the rz2rx decomposition
Addition of test file for the rz2rx decomposition and edit to comments in the rz2rx file
- Loading branch information
1 parent
a37c182
commit 5aab56b
Showing
3 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.pythonPath": "C:\\Users\\daisy\\Anaconda3\\envs\\py36\\python.exe" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
"Tests for projectq.setups.decompositions.rx2rz.py" | ||
|
||
import math | ||
|
||
import pytest | ||
|
||
from projectq import MainEngine | ||
from projectq.backends import Simulator | ||
from projectq.cengines import (AutoReplacer, DecompositionRuleSet, | ||
DummyEngine, InstructionFilter, MainEngine) | ||
from projectq.meta import Control | ||
from projectq.ops import Measure, Ph, Rz | ||
|
||
# from . import rz2rx | ||
import rz2rx | ||
|
||
import sys | ||
|
||
sys.path.insert(0, 'C:\\Users\\daisy\\OneDrive\\Documents\\Sussex masters\\Masters project\\projectQ_practice_branch\\ProjectQ') | ||
|
||
def test_recognize_correct_gates(): | ||
""" Check that Rz gate is not part of a two-qubit control | ||
gate """ | ||
# Creates a circuit and checks that there is only | ||
# a ctrl qubit if you create one in the circuit | ||
saving_backend = DummyEngine(save_commands=True) | ||
eng = MainEngine(backend=saving_backend) | ||
qubit = eng.allocate_qubit() | ||
ctrl_qubit = eng.allocate_qubit() | ||
eng.flush() | ||
Rz(0.3) | qubit | ||
with Control(eng, ctrl_qubit): | ||
Rz(0.4) | qubit | ||
eng.flush(deallocate_qubits=True) | ||
assert rz2rx._recognize_RzNoCtrl(saving_backend.received_commands[3]) | ||
assert not rz2rx._recognize_RzNoCtrl(saving_backend.received_commands[4]) | ||
|
||
|
||
def rz_decomp_gates(eng, cmd): | ||
""" Check that cmd.gate is the gate Rz """ | ||
g = cmd.gate | ||
if isinstance(g, Rz): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
@pytest.mark.parametrize("angle", [0, math.pi, 2*math.pi, 4*math.pi, 0.5]) | ||
def test_decomposition(angle): | ||
""" Test whether the decomposition of Rz results in | ||
the same superposition of |0> and |1> as just using Rz """ | ||
for basis_state in ([1, 0], [0, 1]): | ||
correct_dummy_eng = DummyEngine(save_commands=True) | ||
correct_eng = MainEngine(backend=Simulator(), | ||
engine_list=[correct_dummy_eng]) | ||
|
||
rule_set = DecompositionRuleSet(modules=[rz2rx]) | ||
test_dummy_eng = DummyEngine(save_commands=True) | ||
test_eng = MainEngine(backend=Simulator(), | ||
engine_list=[AutoReplacer(rule_set), | ||
InstructionFilter(rz_decomp_gates), | ||
test_dummy_eng]) | ||
|
||
correct_qb = correct_eng.allocate_qubit() | ||
Rz(angle) | correct_qb | ||
correct_eng.flush() | ||
|
||
test_qb = test_eng.allocate_qubit() | ||
Rz(angle) | test_qb | ||
test_eng.flush() | ||
|
||
assert correct_dummy_eng.received_commands[1].gate == Rz(angle) | ||
assert test_dummy_eng.received_commands[1].gate != Rz(angle) | ||
|
||
for fstate in ['0', '1']: | ||
test = test_eng.backend.get_amplitude(fstate, test_qb) | ||
correct = correct_eng.backend.get_amplitude(fstate, correct_qb) | ||
assert correct == pytest.approx(test, rel=1e-12, abs=1e-12) | ||
|
||
Measure | test_qb | ||
Measure | correct_qb |