-
Notifications
You must be signed in to change notification settings - Fork 985
/
measure_util_test.py
98 lines (78 loc) · 3.55 KB
/
measure_util_test.py
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
# Copyright 2019 The Cirq Developers
#
# 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
#
# https://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.
import numpy as np
import pytest
import cirq
def test_measure_qubits():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
# Empty application.
with pytest.raises(ValueError, match='empty set of qubits'):
_ = cirq.measure()
assert cirq.measure(a) == cirq.MeasurementGate(num_qubits=1, key='a').on(a)
assert cirq.measure(a, b) == cirq.MeasurementGate(num_qubits=2, key='a,b').on(a, b)
assert cirq.measure(b, a) == cirq.MeasurementGate(num_qubits=2, key='b,a').on(b, a)
assert cirq.measure(a, key='b') == cirq.MeasurementGate(num_qubits=1, key='b').on(a)
assert cirq.measure(a, invert_mask=(True,)) == cirq.MeasurementGate(
num_qubits=1, key='a', invert_mask=(True,)
).on(a)
assert cirq.measure(*cirq.LineQid.for_qid_shape((1, 2, 3)), key='a') == cirq.MeasurementGate(
num_qubits=3, key='a', qid_shape=(1, 2, 3)
).on(*cirq.LineQid.for_qid_shape((1, 2, 3)))
with pytest.raises(ValueError, match='ndarray'):
_ = cirq.measure(np.ndarray([1, 0]))
with pytest.raises(ValueError, match='Qid'):
_ = cirq.measure("bork")
def test_measure_each():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
assert cirq.measure_each() == []
assert cirq.measure_each(a) == [cirq.measure(a)]
assert cirq.measure_each(a, b) == [cirq.measure(a), cirq.measure(b)]
assert cirq.measure_each(a.with_dimension(3), b.with_dimension(3)) == [
cirq.measure(a.with_dimension(3)),
cirq.measure(b.with_dimension(3)),
]
assert cirq.measure_each(a, b, key_func=lambda e: e.name + '!') == [
cirq.measure(a, key='a!'),
cirq.measure(b, key='b!'),
]
def test_measure_single_paulistring():
# Correct application
q = cirq.LineQubit.range(3)
ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
assert cirq.measure_single_paulistring(ps, key='a') == cirq.PauliMeasurementGate(
ps.values(), key='a'
).on(*ps.keys())
# Empty application
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
_ = cirq.measure_single_paulistring(cirq.I(q[0]) * cirq.I(q[1]))
# Wrong type
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
_ = cirq.measure_single_paulistring(q)
def test_measure_paulistring_terms():
# Correct application
q = cirq.LineQubit.range(3)
ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2])
assert cirq.measure_paulistring_terms(ps) == [
cirq.PauliMeasurementGate([cirq.X], key=str(q[0])).on(q[0]),
cirq.PauliMeasurementGate([cirq.Y], key=str(q[1])).on(q[1]),
cirq.PauliMeasurementGate([cirq.Z], key=str(q[2])).on(q[2]),
]
# Empty application
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
_ = cirq.measure_paulistring_terms(cirq.I(q[0]) * cirq.I(q[1]))
# Wrong type
with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'):
_ = cirq.measure_paulistring_terms(q)