-
Notifications
You must be signed in to change notification settings - Fork 986
/
frozen_circuit_test.py
76 lines (61 loc) · 2.21 KB
/
frozen_circuit_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
# Copyright 2020 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.
"""Tests exclusively for FrozenCircuits.
Behavior shared with Circuit is tested with parameters in circuit_test.py.
"""
import pytest
import cirq
def test_from_moments():
a, b, c, d = cirq.LineQubit.range(4)
assert cirq.FrozenCircuit.from_moments(
[cirq.X(a), cirq.Y(b)],
[cirq.X(c)],
[],
cirq.Z(d),
[cirq.measure(a, b, key='ab'), cirq.measure(c, d, key='cd')],
) == cirq.FrozenCircuit(
cirq.Moment(cirq.X(a), cirq.Y(b)),
cirq.Moment(cirq.X(c)),
cirq.Moment(),
cirq.Moment(cirq.Z(d)),
cirq.Moment(cirq.measure(a, b, key='ab'), cirq.measure(c, d, key='cd')),
)
def test_freeze_and_unfreeze():
a, b = cirq.LineQubit.range(2)
c = cirq.Circuit(cirq.X(a), cirq.H(b))
f = c.freeze()
# Circuits equal their frozen versions, similar to set(x) == frozenset(x).
assert f == c
assert cirq.approx_eq(f, c)
# Freezing a FrozenCircuit will return the original.
ff = f.freeze()
assert ff is f
unf = f.unfreeze()
assert unf.moments == c.moments
assert unf is not c
# Unfreezing always returns a copy.
cc = c.unfreeze()
assert cc is not c
fcc = cc.freeze()
assert fcc.moments == f.moments
assert fcc is not f
def test_immutable():
q = cirq.LineQubit(0)
c = cirq.FrozenCircuit(cirq.X(q), cirq.H(q))
# Match one of two strings. The second one is message returned since python 3.11.
with pytest.raises(
AttributeError,
match="(can't set attribute)|(property 'moments' of 'FrozenCircuit' object has no setter)",
):
c.moments = (cirq.Moment(cirq.H(q)), cirq.Moment(cirq.X(q)))