|
40 | 40 | from qutip.tensor import tensor
|
41 | 41 |
|
42 | 42 |
|
43 |
| -def TestBasicPulse(): |
44 |
| - """ |
45 |
| - Test for basic pulse generation and attributes. |
46 |
| - """ |
47 |
| - coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
48 |
| - tlist = np.array([0., 1., 2., 3.]) |
49 |
| - ham = sigmaz() |
50 |
| - |
51 |
| - # Basic tests |
52 |
| - pulse1 = Pulse(ham, 1, tlist, coeff) |
53 |
| - assert_allclose( |
54 |
| - pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
55 |
| - pulse1.tlist = 2 * tlist |
56 |
| - assert_allclose(pulse1.tlist, 2 * tlist) |
57 |
| - pulse1.tlist = tlist |
58 |
| - pulse1.coeff = 2 * coeff |
59 |
| - assert_allclose(pulse1.coeff, 2 * coeff) |
60 |
| - pulse1.coeff = coeff |
61 |
| - pulse1.qobj = 2 * sigmay() |
62 |
| - assert_allclose(pulse1.qobj, 2 * sigmay()) |
63 |
| - pulse1.qobj = ham |
64 |
| - pulse1.targets = 3 |
65 |
| - assert_allclose(pulse1.targets, 3) |
66 |
| - pulse1.targets = 1 |
67 |
| - assert_allclose(pulse1.get_ideal_qobj(2), tensor(identity(2), sigmaz())) |
68 |
| - |
69 |
| - |
70 |
| -def TestCoherentNoise(): |
71 |
| - """ |
72 |
| - Test for pulse genration with coherent noise. |
73 |
| - """ |
74 |
| - coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
75 |
| - tlist = np.array([0., 1., 2., 3.]) |
76 |
| - ham = sigmaz() |
77 |
| - pulse1 = Pulse(ham, 1, tlist, coeff) |
78 |
| - # Add coherent noise with the same tlist |
79 |
| - pulse1.add_coherent_noise(sigmay(), 0, tlist, coeff) |
80 |
| - assert_allclose( |
81 |
| - pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
82 |
| - assert_(len(pulse1.coherent_noise) == 1) |
83 |
| - noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) |
84 |
| - assert_allclose(c_ops, []) |
85 |
| - assert_allclose(noise_qu.tlist, np.array([0., 1., 2., 3.])) |
86 |
| - qobj_list = [ele.qobj for ele in noise_qu.ops] |
87 |
| - assert_(tensor(identity(2), sigmaz()) in qobj_list) |
88 |
| - assert_(tensor(sigmay(), identity(2)) in qobj_list) |
89 |
| - for ele in noise_qu.ops: |
90 |
| - assert_allclose(ele.coeff, coeff) |
91 |
| - |
92 |
| - |
93 |
| -def TestNoisyPulse(): |
94 |
| - """ |
95 |
| - Test for lindblad noise and different tlist |
96 |
| - """ |
97 |
| - coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
98 |
| - tlist = np.array([0., 1., 2., 3.]) |
99 |
| - ham = sigmaz() |
100 |
| - pulse1 = Pulse(ham, 1, tlist, coeff) |
101 |
| - # Add coherent noise and lindblad noise with different tlist |
102 |
| - pulse1.spline_kind = "step_func" |
103 |
| - tlist_noise = np.array([1., 2.5, 3.]) |
104 |
| - coeff_noise = np.array([0.5, 0.1, 0.5]) |
105 |
| - pulse1.add_coherent_noise(sigmay(), 0, tlist_noise, coeff_noise) |
106 |
| - tlist_noise2 = np.array([0.5, 2, 3.]) |
107 |
| - coeff_noise2 = np.array([0.1, 0.2, 0.3]) |
108 |
| - pulse1.add_lindblad_noise(sigmax(), 1, coeff=True) |
109 |
| - pulse1.add_lindblad_noise( |
110 |
| - sigmax(), 0, tlist=tlist_noise2, coeff=coeff_noise2) |
111 |
| - |
112 |
| - assert_allclose( |
113 |
| - pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
114 |
| - noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) |
115 |
| - assert_allclose(noise_qu.tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) |
116 |
| - for ele in noise_qu.ops: |
117 |
| - if ele.qobj == tensor(identity(2), sigmaz()): |
118 |
| - assert_allclose( |
119 |
| - ele.coeff, np.array([0.1, 0.1, 0.2, 0.3, 0.3, 0.4])) |
120 |
| - elif ele.qobj == tensor(sigmay(), identity(2)): |
121 |
| - assert_allclose( |
122 |
| - ele.coeff, np.array([0., 0., 0.5, 0.5, 0.1, 0.5])) |
123 |
| - for c_op in c_ops: |
124 |
| - if len(c_op.ops) == 0: |
125 |
| - assert_allclose(c_ops[0].cte, tensor(identity(2), sigmax())) |
126 |
| - else: |
127 |
| - assert_allclose( |
128 |
| - c_ops[1].ops[0].qobj, tensor(sigmax(), identity(2))) |
129 |
| - assert_allclose( |
130 |
| - c_ops[1].tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) |
131 |
| - assert_allclose( |
132 |
| - c_ops[1].ops[0].coeff, np.array([0., 0.1, 0.1, 0.2, 0.2, 0.3])) |
133 |
| - |
134 |
| - |
135 |
| -def TestPulseConstructor(): |
136 |
| - """ |
137 |
| - Test for creating empty Pulse, Pulse with constant coefficients etc. |
138 |
| - """ |
139 |
| - coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
140 |
| - tlist = np.array([0., 1., 2., 3.]) |
141 |
| - ham = sigmaz() |
142 |
| - # Special ways of initializing pulse |
143 |
| - pulse2 = Pulse(sigmax(), 0, tlist, True) |
144 |
| - assert_allclose(pulse2.get_ideal_qobjevo(2).ops[0].qobj, |
145 |
| - tensor(sigmax(), identity(2))) |
146 |
| - |
147 |
| - pulse3 = Pulse(sigmay(), 0) |
148 |
| - assert_allclose(pulse3.get_ideal_qobjevo(2).cte.norm(), 0.) |
149 |
| - |
150 |
| - pulse4 = Pulse(None, None) # Dummy empty ham |
151 |
| - assert_allclose(pulse4.get_ideal_qobjevo(2).cte.norm(), 0.) |
152 |
| - |
153 |
| - tlist_noise = np.array([1., 2.5, 3.]) |
154 |
| - coeff_noise = np.array([0.5, 0.1, 0.5]) |
155 |
| - tlist_noise2 = np.array([0.5, 2, 3.]) |
156 |
| - coeff_noise2 = np.array([0.1, 0.2, 0.3]) |
157 |
| - # Pulse with different dims |
158 |
| - random_qobj = Qobj(np.random.random((3, 3))) |
159 |
| - pulse5 = Pulse(sigmaz(), 1, tlist, True) |
160 |
| - pulse5.add_coherent_noise(sigmay(), 1, tlist_noise, coeff_noise) |
161 |
| - pulse5.add_lindblad_noise( |
162 |
| - random_qobj, 0, tlist=tlist_noise2, coeff=coeff_noise2) |
163 |
| - qu, c_ops = pulse5.get_noisy_qobjevo(dims=[3, 2]) |
164 |
| - assert_allclose(qu.ops[0].qobj, tensor([identity(3), sigmaz()])) |
165 |
| - assert_allclose(qu.ops[1].qobj, tensor([identity(3), sigmay()])) |
166 |
| - assert_allclose(c_ops[0].ops[0].qobj, tensor([random_qobj, identity(2)])) |
167 |
| - |
168 |
| - |
169 |
| -def TestDrift(): |
170 |
| - """ |
171 |
| - Test for Drift |
172 |
| - """ |
173 |
| - drift = Drift() |
174 |
| - assert_allclose(drift.get_ideal_qobjevo(2).cte.norm(), 0) |
175 |
| - drift.add_drift(sigmaz(), targets=1) |
176 |
| - assert_allclose( |
177 |
| - drift.get_ideal_qobjevo(dims=[3, 2]).cte, tensor(identity(3), sigmaz())) |
| 43 | +class TestPulse: |
| 44 | + def testBasicPulse(self): |
| 45 | + """ |
| 46 | + Test for basic pulse generation and attributes. |
| 47 | + """ |
| 48 | + coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
| 49 | + tlist = np.array([0., 1., 2., 3.]) |
| 50 | + ham = sigmaz() |
| 51 | + |
| 52 | + # Basic tests |
| 53 | + pulse1 = Pulse(ham, 1, tlist, coeff) |
| 54 | + assert_allclose( |
| 55 | + pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
| 56 | + pulse1.tlist = 2 * tlist |
| 57 | + assert_allclose(pulse1.tlist, 2 * tlist) |
| 58 | + pulse1.tlist = tlist |
| 59 | + pulse1.coeff = 2 * coeff |
| 60 | + assert_allclose(pulse1.coeff, 2 * coeff) |
| 61 | + pulse1.coeff = coeff |
| 62 | + pulse1.qobj = 2 * sigmay() |
| 63 | + assert_allclose(pulse1.qobj, 2 * sigmay()) |
| 64 | + pulse1.qobj = ham |
| 65 | + pulse1.targets = 3 |
| 66 | + assert_allclose(pulse1.targets, 3) |
| 67 | + pulse1.targets = 1 |
| 68 | + assert_allclose(pulse1.get_ideal_qobj(2), tensor(identity(2), sigmaz())) |
| 69 | + |
| 70 | + |
| 71 | + def testCoherentNoise(self): |
| 72 | + """ |
| 73 | + Test for pulse genration with coherent noise. |
| 74 | + """ |
| 75 | + coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
| 76 | + tlist = np.array([0., 1., 2., 3.]) |
| 77 | + ham = sigmaz() |
| 78 | + pulse1 = Pulse(ham, 1, tlist, coeff) |
| 79 | + # Add coherent noise with the same tlist |
| 80 | + pulse1.add_coherent_noise(sigmay(), 0, tlist, coeff) |
| 81 | + assert_allclose( |
| 82 | + pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
| 83 | + assert_(len(pulse1.coherent_noise) == 1) |
| 84 | + noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) |
| 85 | + assert_allclose(c_ops, []) |
| 86 | + assert_allclose(noise_qu.tlist, np.array([0., 1., 2., 3.])) |
| 87 | + qobj_list = [ele.qobj for ele in noise_qu.ops] |
| 88 | + assert_(tensor(identity(2), sigmaz()) in qobj_list) |
| 89 | + assert_(tensor(sigmay(), identity(2)) in qobj_list) |
| 90 | + for ele in noise_qu.ops: |
| 91 | + assert_allclose(ele.coeff, coeff) |
| 92 | + |
| 93 | + |
| 94 | + def testNoisyPulse(self): |
| 95 | + """ |
| 96 | + Test for lindblad noise and different tlist |
| 97 | + """ |
| 98 | + coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
| 99 | + tlist = np.array([0., 1., 2., 3.]) |
| 100 | + ham = sigmaz() |
| 101 | + pulse1 = Pulse(ham, 1, tlist, coeff) |
| 102 | + # Add coherent noise and lindblad noise with different tlist |
| 103 | + pulse1.spline_kind = "step_func" |
| 104 | + tlist_noise = np.array([1., 2.5, 3.]) |
| 105 | + coeff_noise = np.array([0.5, 0.1, 0.5]) |
| 106 | + pulse1.add_coherent_noise(sigmay(), 0, tlist_noise, coeff_noise) |
| 107 | + tlist_noise2 = np.array([0.5, 2, 3.]) |
| 108 | + coeff_noise2 = np.array([0.1, 0.2, 0.3]) |
| 109 | + pulse1.add_lindblad_noise(sigmax(), 1, coeff=True) |
| 110 | + pulse1.add_lindblad_noise( |
| 111 | + sigmax(), 0, tlist=tlist_noise2, coeff=coeff_noise2) |
| 112 | + |
| 113 | + assert_allclose( |
| 114 | + pulse1.get_ideal_qobjevo(2).ops[0].qobj, tensor(identity(2), sigmaz())) |
| 115 | + noise_qu, c_ops = pulse1.get_noisy_qobjevo(2) |
| 116 | + assert_allclose(noise_qu.tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) |
| 117 | + for ele in noise_qu.ops: |
| 118 | + if ele.qobj == tensor(identity(2), sigmaz()): |
| 119 | + assert_allclose( |
| 120 | + ele.coeff, np.array([0.1, 0.1, 0.2, 0.3, 0.3, 0.4])) |
| 121 | + elif ele.qobj == tensor(sigmay(), identity(2)): |
| 122 | + assert_allclose( |
| 123 | + ele.coeff, np.array([0., 0., 0.5, 0.5, 0.1, 0.5])) |
| 124 | + for c_op in c_ops: |
| 125 | + if len(c_op.ops) == 0: |
| 126 | + assert_allclose(c_ops[0].cte, tensor(identity(2), sigmax())) |
| 127 | + else: |
| 128 | + assert_allclose( |
| 129 | + c_ops[1].ops[0].qobj, tensor(sigmax(), identity(2))) |
| 130 | + assert_allclose( |
| 131 | + c_ops[1].tlist, np.array([0., 0.5, 1., 2., 2.5, 3.])) |
| 132 | + assert_allclose( |
| 133 | + c_ops[1].ops[0].coeff, np.array([0., 0.1, 0.1, 0.2, 0.2, 0.3])) |
| 134 | + |
| 135 | + |
| 136 | + def testPulseConstructor(self): |
| 137 | + """ |
| 138 | + Test for creating empty Pulse, Pulse with constant coefficients etc. |
| 139 | + """ |
| 140 | + coeff = np.array([0.1, 0.2, 0.3, 0.4]) |
| 141 | + tlist = np.array([0., 1., 2., 3.]) |
| 142 | + ham = sigmaz() |
| 143 | + # Special ways of initializing pulse |
| 144 | + pulse2 = Pulse(sigmax(), 0, tlist, True) |
| 145 | + assert_allclose(pulse2.get_ideal_qobjevo(2).ops[0].qobj, |
| 146 | + tensor(sigmax(), identity(2))) |
| 147 | + |
| 148 | + pulse3 = Pulse(sigmay(), 0) |
| 149 | + assert_allclose(pulse3.get_ideal_qobjevo(2).cte.norm(), 0.) |
| 150 | + |
| 151 | + pulse4 = Pulse(None, None) # Dummy empty ham |
| 152 | + assert_allclose(pulse4.get_ideal_qobjevo(2).cte.norm(), 0.) |
| 153 | + |
| 154 | + tlist_noise = np.array([1., 2.5, 3.]) |
| 155 | + coeff_noise = np.array([0.5, 0.1, 0.5]) |
| 156 | + tlist_noise2 = np.array([0.5, 2, 3.]) |
| 157 | + coeff_noise2 = np.array([0.1, 0.2, 0.3]) |
| 158 | + # Pulse with different dims |
| 159 | + random_qobj = Qobj(np.random.random((3, 3))) |
| 160 | + pulse5 = Pulse(sigmaz(), 1, tlist, True) |
| 161 | + pulse5.add_coherent_noise(sigmay(), 1, tlist_noise, coeff_noise) |
| 162 | + pulse5.add_lindblad_noise( |
| 163 | + random_qobj, 0, tlist=tlist_noise2, coeff=coeff_noise2) |
| 164 | + qu, c_ops = pulse5.get_noisy_qobjevo(dims=[3, 2]) |
| 165 | + assert_allclose(qu.ops[0].qobj, tensor([identity(3), sigmaz()])) |
| 166 | + assert_allclose(qu.ops[1].qobj, tensor([identity(3), sigmay()])) |
| 167 | + assert_allclose(c_ops[0].ops[0].qobj, tensor([random_qobj, identity(2)])) |
| 168 | + |
| 169 | + |
| 170 | + def testDrift(self): |
| 171 | + """ |
| 172 | + Test for Drift |
| 173 | + """ |
| 174 | + drift = Drift() |
| 175 | + assert_allclose(drift.get_ideal_qobjevo(2).cte.norm(), 0) |
| 176 | + drift.add_drift(sigmaz(), targets=1) |
| 177 | + assert_allclose( |
| 178 | + drift.get_ideal_qobjevo(dims=[3, 2]).cte, tensor(identity(3), sigmaz())) |
178 | 179 |
|
179 | 180 |
|
180 | 181 | if __name__ == "__main__":
|
|
0 commit comments