-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy path_control_test.py
More file actions
executable file
·178 lines (148 loc) · 7.02 KB
/
_control_test.py
File metadata and controls
executable file
·178 lines (148 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright 2017 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for projectq.meta._control.py"""
import pytest
from projectq import MainEngine
from projectq.cengines import DummyEngine
from projectq.meta import (
Compute,
ComputeTag,
DirtyQubitTag,
Uncompute,
UncomputeTag,
_control,
)
from projectq.ops import Command, CtrlAll, H, IncompatibleControlState, Rx, X
from projectq.types import WeakQubitRef
def test_canonical_representation():
assert _control.canonical_ctrl_state(0, 0) == ''
for num_qubits in range(4):
assert _control.canonical_ctrl_state(0, num_qubits) == '0' * num_qubits
num_qubits = 4
for i in range(2**num_qubits):
state = f'{i:0b}'.zfill(num_qubits)
assert _control.canonical_ctrl_state(i, num_qubits) == state[::-1]
assert _control.canonical_ctrl_state(state, num_qubits) == state
for num_qubits in range(10):
assert _control.canonical_ctrl_state(CtrlAll.Zero, num_qubits) == '0' * num_qubits
assert _control.canonical_ctrl_state(CtrlAll.One, num_qubits) == '1' * num_qubits
with pytest.raises(TypeError):
_control.canonical_ctrl_state(1.1, 2)
with pytest.raises(ValueError):
_control.canonical_ctrl_state('1', 2)
with pytest.raises(ValueError):
_control.canonical_ctrl_state('11111', 2)
with pytest.raises(ValueError):
_control.canonical_ctrl_state('1a', 2)
with pytest.raises(ValueError):
_control.canonical_ctrl_state(4, 2)
def test_has_negative_control():
qubit0 = WeakQubitRef(None, 0)
qubit1 = WeakQubitRef(None, 0)
qubit2 = WeakQubitRef(None, 0)
qubit3 = WeakQubitRef(None, 0)
assert not _control.has_negative_control(Command(None, H, ([qubit0],)))
assert not _control.has_negative_control(Command(None, H, ([qubit0],), [qubit1]))
assert not _control.has_negative_control(Command(None, H, ([qubit0],), [qubit1], control_state=CtrlAll.One))
assert _control.has_negative_control(Command(None, H, ([qubit0],), [qubit1], control_state=CtrlAll.Zero))
assert _control.has_negative_control(
Command(None, H, ([qubit0],), [qubit1, qubit2, qubit3], control_state=CtrlAll.Zero)
)
assert not _control.has_negative_control(
Command(None, H, ([qubit0],), [qubit1, qubit2, qubit3], control_state='111')
)
assert _control.has_negative_control(Command(None, H, ([qubit0],), [qubit1, qubit2, qubit3], control_state='101'))
def test_control_engine_has_compute_tag():
eng = MainEngine(backend=DummyEngine(), engine_list=[DummyEngine()])
qubit = eng.allocate_qubit()
test_cmd0 = Command(eng, H, (qubit,))
test_cmd1 = Command(eng, H, (qubit,))
test_cmd2 = Command(eng, H, (qubit,))
test_cmd0.tags = [DirtyQubitTag(), ComputeTag(), DirtyQubitTag()]
test_cmd1.tags = [DirtyQubitTag(), UncomputeTag(), DirtyQubitTag()]
test_cmd2.tags = [DirtyQubitTag()]
assert _control._has_compute_uncompute_tag(test_cmd0)
assert _control._has_compute_uncompute_tag(test_cmd1)
assert not _control._has_compute_uncompute_tag(test_cmd2)
def test_control():
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend, engine_list=[DummyEngine()])
qureg = eng.allocate_qureg(2)
with _control.Control(eng, qureg):
qubit = eng.allocate_qubit()
with Compute(eng):
Rx(0.5) | qubit
H | qubit
Uncompute(eng)
with _control.Control(eng, qureg[0]):
H | qubit
eng.flush()
assert len(backend.received_commands) == 8
assert len(backend.received_commands[0].control_qubits) == 0
assert len(backend.received_commands[1].control_qubits) == 0
assert len(backend.received_commands[2].control_qubits) == 0
assert len(backend.received_commands[3].control_qubits) == 0
assert len(backend.received_commands[4].control_qubits) == 2
assert len(backend.received_commands[5].control_qubits) == 0
assert len(backend.received_commands[6].control_qubits) == 1
assert len(backend.received_commands[7].control_qubits) == 0
assert backend.received_commands[4].control_qubits[0].id == qureg[0].id
assert backend.received_commands[4].control_qubits[1].id == qureg[1].id
assert backend.received_commands[6].control_qubits[0].id == qureg[0].id
with pytest.raises(TypeError):
_control.Control(eng, (qureg[0], qureg[1]))
def test_control_state():
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend, engine_list=[DummyEngine()])
qureg = eng.allocate_qureg(3)
xreg = eng.allocate_qureg(3)
X | qureg[1]
with _control.Control(eng, qureg[0], '0'):
with Compute(eng):
X | xreg[0]
X | xreg[1]
Uncompute(eng)
with _control.Control(eng, qureg[1:], 2):
X | xreg[2]
eng.flush()
assert len(backend.received_commands) == 6 + 5 + 1
assert len(backend.received_commands[0].control_qubits) == 0
assert len(backend.received_commands[1].control_qubits) == 0
assert len(backend.received_commands[2].control_qubits) == 0
assert len(backend.received_commands[3].control_qubits) == 0
assert len(backend.received_commands[4].control_qubits) == 0
assert len(backend.received_commands[5].control_qubits) == 0
assert len(backend.received_commands[6].control_qubits) == 0
assert len(backend.received_commands[7].control_qubits) == 0
assert len(backend.received_commands[8].control_qubits) == 1
assert len(backend.received_commands[9].control_qubits) == 0
assert len(backend.received_commands[10].control_qubits) == 2
assert len(backend.received_commands[11].control_qubits) == 0
assert backend.received_commands[8].control_qubits[0].id == qureg[0].id
assert backend.received_commands[8].control_state == '0'
assert backend.received_commands[10].control_qubits[0].id == qureg[1].id
assert backend.received_commands[10].control_qubits[1].id == qureg[2].id
assert backend.received_commands[10].control_state == '01'
assert _control.has_negative_control(backend.received_commands[8])
assert _control.has_negative_control(backend.received_commands[10])
def test_control_state_contradiction():
backend = DummyEngine(save_commands=True)
eng = MainEngine(backend=backend, engine_list=[DummyEngine()])
qureg = eng.allocate_qureg(1)
with pytest.raises(IncompatibleControlState):
with _control.Control(eng, qureg[0], '0'):
qubit = eng.allocate_qubit()
with _control.Control(eng, qureg[0], '1'):
H | qubit
eng.flush()