microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7421e7dd1015dcbd940bf843d33583470de580ea

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/tests/test_noisy_simulator.py

216lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4from qsharp.noisy_simulator import (
5 NoisySimulatorError,
6 Operation,
7 Instrument,
8 DensityMatrixSimulator,
9 StateVectorSimulator,
10)
11import pytest
12
13
14# Tests for the Q# noisy simulator.
15
16
17def test_matrices_are_not_transposed_when_returned_back_to_python():
18 """
19 This test is due to the performance optimization we make in
20 `noisy_simulator/src/operation.rs/Operation::new`, we want to check that
21 we are reversing the transpose we made there before returning to Python.
22 """
23 # This is one of the Kraus operators of the depolarizing channel,
24 # so it is a valid Kraus operator.
25 op = Operation([[[0j, -0.5j], [0.5j, 0j]]])
26 assert op.get_kraus_operators() == [[[0j, -0.5j], [0.5j, 0j]]]
27
28
29# Operation tests
30
31
32def test_operation_number_of_qubits_is_mapped_correctly():
33 op = Operation([[[1, 0], [0, 0]]])
34 assert op.get_number_of_qubits() == 1
35
36
37def test_operation_kraus_operators_are_mapped_correctly():
38 op = Operation([[[1, 0], [0, 0]]])
39 assert op.get_kraus_operators() == [[[(1 + 0j), 0j], [0j, 0j]]]
40
41
42def test_operation_effect_matrix_is_mapped_correctly():
43 op = Operation([[[1, 0], [0, 0]]])
44 assert op.get_effect_matrix() == [[(1 + 0j), 0j], [0j, 0j]]
45
46
47def test_operation_matrix_is_mapped_correctly():
48 op = Operation([[[1, 0], [0, 0]]])
49 assert op.get_operation_matrix() == [
50 [(1 + 0j), 0j, 0j, 0j],
51 [0j, 0j, 0j, 0j],
52 [0j, 0j, 0j, 0j],
53 [0j, 0j, 0j, 0j],
54 ]
55
56
57def test_constructing_an_empty_operation_throws_exception():
58 with pytest.raises(NoisySimulatorError) as excinfo:
59 _ = Operation([])
60 assert (
61 str(excinfo.value)
62 == "error when building operation: there should be at least one Kraus Operator"
63 )
64
65
66def test_constructed_an_ill_formed_operation_throws_exception():
67 with pytest.raises(NoisySimulatorError) as excinfo:
68 op = Operation([[[1, 0], [0, 0, 0]]])
69 assert str(excinfo.value) == "ill formed matrix, all rows should be the same length"
70
71
72# Instrument tests
73
74
75def test_constructing_an_instrument_with_a_valid_operation_succeeds():
76 op = Operation([[[1, 0], [0, 0]]])
77 _ = Instrument([op])
78
79
80def test_constructing_an_ill_formed_instrument_throws_exception():
81 op0 = Operation([[[1, 0], [0, 0]]])
82 op1 = Operation(
83 [
84 [
85 [1, 0, 0, 0],
86 [0, 0, 0, 0],
87 [0, 0, 0, 0],
88 [0, 0, 0, 0],
89 ]
90 ]
91 )
92
93 with pytest.raises(NoisySimulatorError) as excinfo:
94 _ = Instrument([op0, op1])
95
96 assert (
97 str(excinfo.value)
98 == "error when building instrument: all Operations should target the same number of qubits"
99 )
100
101
102def test_constructing_an_empty_instrument_throws_exception():
103 with pytest.raises(NoisySimulatorError) as excinfo:
104 inst = Instrument([])
105 assert (
106 str(excinfo.value)
107 == "error when building instrument: there should be at least one Operation"
108 )
109
110
111# DensityMatrixSimulator tests
112
113
114def test_density_matrix_simulator_apply_operation_is_mapped_correctly():
115 op = Operation([[[1, 0], [0, 0]]])
116 sim = DensityMatrixSimulator(1, seed=42)
117 sim.apply_operation(op, [0])
118
119
120def test_density_matrix_simulator_apply_instrument_is_mapped_correctly():
121 mz0 = Operation([[[1, 0], [0, 0]]])
122 mz = Instrument([mz0])
123 sim = DensityMatrixSimulator(1, seed=42)
124 sim.apply_instrument(mz, [0])
125
126
127def test_density_matrix_simulator_sample_instrument_is_mapped_correctly():
128 mz0 = Operation([[[1, 0], [0, 0]]])
129 mz = Instrument([mz0])
130 sim = DensityMatrixSimulator(1, seed=42)
131 assert 0 == sim.sample_instrument(mz, [0])
132
133
134def test_density_matrix_simulator_get_state_is_mapped_correctly():
135 sim = DensityMatrixSimulator(1)
136 assert sim.get_state().data() == [[1, 0], [0, 0]]
137
138
139def test_density_matrix_simulator_set_state_is_mapped_correctly():
140 f = 0.5**0.5
141 h = Operation([[[f, f], [f, -f]]])
142 sim = DensityMatrixSimulator(1)
143 inital_state = sim.get_state()
144 sim.apply_operation(h, [0])
145 sim.set_state(inital_state)
146 assert sim.get_state().data() == [[1, 0], [0, 0]]
147
148
149def test_density_matrix_simulator_set_trace_is_mapped_correctly():
150 sim = DensityMatrixSimulator(1)
151 sim.set_trace(0.5)
152
153
154def test_density_matrix_simulator_out_of_bounds_qubit():
155 f = 0.5**0.5
156 h = Operation([[[f, f], [f, -f]]])
157 sim = DensityMatrixSimulator(1)
158
159 with pytest.raises(NoisySimulatorError) as excinfo:
160 sim.apply_operation(h, [1])
161
162 assert str(excinfo.value) == "qubit id out of bounds: 1"
163
164
165# StateVectorSimulator tests
166
167
168def test_state_vector_simulator_apply_operation_is_mapped_correctly():
169 op = Operation([[[1, 0], [0, 0]]])
170 sim = StateVectorSimulator(1, seed=42)
171 sim.apply_operation(op, [0])
172
173
174def test_state_vector_simulator_apply_instrument_is_mapped_correctly():
175 mz0 = Operation([[[1, 0], [0, 0]]])
176 mz = Instrument([mz0])
177 sim = StateVectorSimulator(1, seed=42)
178 sim.apply_instrument(mz, [0])
179
180
181def test_state_vector_simulator_sample_instrument_is_mapped_correctly():
182 mz0 = Operation([[[1, 0], [0, 0]]])
183 mz = Instrument([mz0])
184 sim = StateVectorSimulator(1, seed=42)
185 assert 0 == sim.sample_instrument(mz, [0])
186
187
188def test_state_vector_simulator_get_state_is_mapped_correctly():
189 sim = StateVectorSimulator(1)
190 assert sim.get_state().data() == [1, 0]
191
192
193def test_state_vector_simulator_set_state_is_mapped_correctly():
194 f = 0.5**0.5
195 h = Operation([[[f, f], [f, -f]]])
196 sim = StateVectorSimulator(1)
197 inital_state = sim.get_state()
198 sim.apply_operation(h, [0])
199 sim.set_state(inital_state)
200 assert sim.get_state().data() == [1, 0]
201
202
203def test_state_vector_simulator_set_trace_is_mapped_correctly():
204 sim = StateVectorSimulator(1)
205 sim.set_trace(0.5)
206
207
208def test_state_vector_simulator_out_of_bounds_qubit():
209 f = 0.5**0.5
210 h = Operation([[[f, f], [f, -f]]])
211 sim = StateVectorSimulator(1)
212
213 with pytest.raises(NoisySimulatorError) as excinfo:
214 sim.apply_operation(h, [1])
215
216 assert str(excinfo.value) == "qubit id out of bounds: 1"