@@ -8,7 +8,7 @@ using cuStateVec
8
8
@info " cuStateVec version: $(cuStateVec. version ()) "
9
9
10
10
@testset " cuStateVec" begin
11
- import cuStateVec: CuStateVec, applyMatrix!, applyPauliExp!, applyGeneralizedPermutationMatrix!, expectation, expectationsOnPauliBasis, sample, testMatrixType, Pauli, PauliX, PauliY, PauliZ, PauliI, measureOnZBasis!, swapIndexBits!
11
+ import cuStateVec: CuStateVec, applyMatrix!, applyMatrixBatched!, applyPauliExp!, applyGeneralizedPermutationMatrix!, expectation, expectationsOnPauliBasis, sample, testMatrixType, Pauli, PauliX, PauliY, PauliZ, PauliI, measureOnZBasis!, swapIndexBits!, abs2SumOnZBasis, collapseOnZBasis!, batchMeasure!, abs2SumArray, collapseByBitString!, abs2SumArrayBatched, collapseByBitStringBatched !
12
12
13
13
@testset " applyMatrix! and expectation" begin
14
14
# build a simple state and compute expectations
@@ -19,7 +19,8 @@ using cuStateVec
19
19
Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
20
20
sv = CuStateVec (elty, n_q)
21
21
sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
22
- exp, res = expectation (sv, Z, Int32[0 ])
22
+ sv = applyMatrix! (sv, H, false , Int32[1 ], Int32[])
23
+ exp, res = expectation (sv, Z, Int32[1 ])
23
24
@test exp ≈ 0.0 atol= 1e-6
24
25
exp, res = expectation (sv, X, Int32[0 ])
25
26
@test exp ≈ 1.0 atol= 1e-6
@@ -38,6 +39,69 @@ using cuStateVec
38
39
exp, res = expectation (sv, X, Int32[0 ])
39
40
@test exp ≈ 0.0 atol= 1e-6
40
41
end
42
+ # with expectationsOnPauliBasis
43
+ n_q = 2
44
+ @testset for elty in [ComplexF32, ComplexF64]
45
+ H = convert (Matrix{elty}, (1 /√ 2 ). * [1 1 ; 1 - 1 ])
46
+ X = convert (Matrix{elty}, [0 1 ; 1 0 ])
47
+ Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
48
+ sv = CuStateVec (elty, n_q)
49
+ sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
50
+ sv = applyMatrix! (sv, H, false , Int32[1 ], Int32[])
51
+ pauli_ops = [cuStateVec. Pauli[cuStateVec. PauliX ()], cuStateVec. Pauli[cuStateVec. PauliX ()]]
52
+ exp_vals = expectationsOnPauliBasis (sv, pauli_ops, [[0 ], [1 ]])
53
+ @test exp_vals[1 ] ≈ 1.0 atol= 1e-6
54
+ @test exp_vals[2 ] ≈ 1.0 atol= 1e-6
55
+ end
56
+ end
57
+ @testset " applyMatrixBatched! and expectation" begin
58
+ # build a simple state and compute expectations
59
+ n_q = 2
60
+ @testset for elty in [ComplexF32, ComplexF64]
61
+ H = convert (Matrix{elty}, (1 /√ 2 ). * [1 1 ; 1 - 1 ])
62
+ X = convert (Matrix{elty}, [0 1 ; 1 0 ])
63
+ Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
64
+ @testset for n_svs in (1 , 2 )
65
+ @testset for (mapping, mat_inds, n_mats) in (
66
+ (cuStateVec. CUSTATEVEC_MATRIX_MAP_TYPE_MATRIX_INDEXED, collect (0 : n_svs- 1 ), n_svs),
67
+ (cuStateVec. CUSTATEVEC_MATRIX_MAP_TYPE_MATRIX_INDEXED, fill (0 , n_svs), 1 ),
68
+ (cuStateVec. CUSTATEVEC_MATRIX_MAP_TYPE_BROADCAST, fill (0 , n_svs), 1 ),
69
+ )
70
+ batched_vec = CUDA. zeros (elty, n_svs* 2 ^ (n_q))
71
+ for sv_ix in 0 : n_svs- 1
72
+ CUDA. @allowscalar batched_vec[sv_ix* (2 ^ n_q) + 1 ] = one (elty)
73
+ end
74
+ sv = CuStateVec (batched_vec) # padded state vector
75
+ H_batch = CuVector {elty} (repeat (vec (H), n_mats))
76
+ sv = applyMatrixBatched! (sv, n_svs, mapping, mat_inds, H_batch, n_mats, false , Int32[0 ], Int32[])
77
+ CUDA. @allowscalar begin
78
+ for sv_ix in 0 : n_svs- 1
79
+ ix_begin = sv_ix* 2 ^ n_q + 1
80
+ ix_end = (sv_ix+ 1 )* 2 ^ n_q
81
+ sv_ = CuStateVec (sv. data[ix_begin: ix_end])
82
+ exp, res = expectation (sv_, Z, Int32[0 ])
83
+ @test exp ≈ 0.0 atol= 1e-6
84
+ exp, res = expectation (sv_, X, Int32[0 ])
85
+ @test exp ≈ 1.0 atol= 1e-6
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ # build a simple state with controls and compute expectations
92
+ n_q = 2
93
+ @testset for elty in [ComplexF32, ComplexF64]
94
+ H = convert (Matrix{elty}, (1 /√ 2 ). * [1 1 ; 1 - 1 ])
95
+ X = convert (Matrix{elty}, [0 1 ; 1 0 ])
96
+ Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
97
+ sv = CuStateVec (elty, n_q)
98
+ sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
99
+ sv = applyMatrix! (sv, X, false , Int32[1 ], Int32[0 ]) # CNOT
100
+ exp, res = expectation (sv, Z, Int32[0 ])
101
+ @test exp ≈ 0.0 atol= 1e-6
102
+ exp, res = expectation (sv, X, Int32[0 ])
103
+ @test exp ≈ 0.0 atol= 1e-6
104
+ end
41
105
end
42
106
@testset " applyMatrix! and sample" begin
43
107
# build a simple state and compute samples
@@ -74,6 +138,22 @@ using cuStateVec
74
138
@test collect (sv_result. data) ≈ h_sv_result
75
139
end
76
140
end
141
+ @testset " abs2sumOnZBasis and collapseOnZBasis!" begin
142
+ @testset for elty in [ComplexF32, ComplexF64]
143
+ h_sv = 1.0 /√ 8 .* elty[0.0 , im, 0.0 , im, 0.0 , im, 0.0 , im]
144
+ h_sv_result_0 = 1.0 /√ 2 * elty[0.0 , 0.0 , 0.0 , im, 0.0 , im, 0.0 , 0.0 ]
145
+ h_sv_result_1 = 1.0 /√ 2 * elty[0.0 , im, 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , im]
146
+ sv = CuStateVec (h_sv)
147
+ abs2sum0, abs2sum1 = abs2SumOnZBasis (sv, [0 , 1 , 2 ])
148
+ abs2sum = abs2sum0 + abs2sum1
149
+ for (parity, norm, h_sv_result) in ((0 , abs2sum0, h_sv_result_0), (1 , abs2sum1, h_sv_result_1))
150
+ d_sv = copy (sv)
151
+ d_sv = collapseOnZBasis! (d_sv, parity, [0 , 1 , 2 ], norm)
152
+ sv_result = collect (d_sv. data)
153
+ @test sv_result ≈ h_sv_result
154
+ end
155
+ end
156
+ end
77
157
@testset " measureOnZBasis" begin
78
158
@testset for elty in [ComplexF32, ComplexF64]
79
159
h_sv = 1.0 /√ 8 .* elty[0.0 , im, 0.0 , im, 0.0 , im, 0.0 , im]
@@ -84,6 +164,61 @@ using cuStateVec
84
164
@test sv_result ≈ h_sv_result
85
165
end
86
166
end
167
+ @testset " abs2SumArray and collapseByBitString!" begin
168
+ nq = 3
169
+ bit_ordering = [2 , 1 , 0 ]
170
+ @testset for elty in [ComplexF32, ComplexF64]
171
+ h_sv = elty[0.0 , 0.1 * im, 0.1 + 0.1 * im, 0.1 + 0.2 * im, 0.2 + 0.2 * im, 0.3 + 0.3im , 0.3 + 0.4 * im, 0.4 + 0.5 * im]
172
+ h_sv_result = elty[0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.3 + 0.4 * im, 0.0 ]
173
+ sv = CuStateVec (h_sv)
174
+ abs2sum = abs2SumArray (sv, bit_ordering, Int[], Int[])
175
+ bitstr = [1 , 1 , 0 ]
176
+ d_sv = copy (sv)
177
+ d_sv = collapseByBitString! (d_sv, bitstr, bit_ordering, 1. )
178
+ sv_result = collect (d_sv. data)
179
+ @test sv_result ≈ h_sv_result
180
+ end
181
+ end
182
+ @testset " abs2SumArrayBatched" begin
183
+ bit_ordering = [1 ]
184
+ @testset for elty in [ComplexF32, ComplexF64]
185
+ @testset for n_svs in (2 ,)
186
+ h_sv = elty[0.0 , 0.1 * im, 0.1 + 0.1 * im, 0.1 + 0.2 * im, 0.2 + 0.2 * im, 0.3 + 0.3 * im, 0.3 + 0.4 * im, 0.4 + 0.5 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im, 0.25 + 0.25 * im]
187
+ a2s_result = real (elty)[0.27 , 0.73 , 0.5 , 0.5 ]
188
+ sv = CuStateVec (h_sv)
189
+ abs2sum = abs2SumArrayBatched (sv, n_svs, bit_ordering, Int[], Int[])
190
+ @test abs2sum ≈ a2s_result
191
+ end
192
+ end
193
+ end
194
+ @testset " collapseByBitStringBatched!" begin
195
+ bit_ordering = [0 , 1 , 2 ]
196
+ @testset for elty in [ComplexF32, ComplexF64]
197
+ @testset for n_svs in (2 ,)
198
+ h_sv = elty[0.0 , 0.1 * im, 0.1 + 0.1 * im, 0.1 + 0.2 * im, 0.2 + 0.2 * im, 0.3 + 0.3 * im, 0.3 + 0.4 * im, 0.4 + 0.5 * im, 0.0 , 0.1 * im, 0.1 + 0.1 * im, 0.1 + 0.2 * im, 0.2 + 0.2 * im, 0.3 + 0.3 * im, 0.3 + 0.4 * im, 0.4 * 0.5 * im]
199
+ h_sv_result = elty[0.0 , im, 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.6 + 0.8 * im, 0.0 ]
200
+ sv = CuStateVec (h_sv)
201
+ bitstr = [0b001 , 0b110 ]
202
+ d_sv = copy (sv)
203
+ d_sv = collapseByBitStringBatched! (d_sv, n_svs, bitstr, bit_ordering, [0.01 , 0.25 ])
204
+ sv_result = collect (d_sv. data)
205
+ @test sv_result ≈ h_sv_result
206
+ end
207
+ end
208
+ end
209
+ @testset " batchMeasure!" begin
210
+ nq = 3
211
+ bit_ordering = [2 , 1 , 0 ]
212
+ @testset for elty in [ComplexF32, ComplexF64]
213
+ h_sv = elty[0.0 , 0.1 * im, 0.1 + 0.1 * im, 0.1 + 0.2 * im, 0.2 + 0.2 * im, 0.3 + 0.3im , 0.3 + 0.4 * im, 0.4 + 0.5 * im]
214
+ h_sv_result = elty[0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.6 + 0.8 * im, 0.0 ]
215
+ sv = CuStateVec (h_sv)
216
+ sv, bitstr = batchMeasure! (sv, bit_ordering, 0.5 , cuStateVec. CUSTATEVEC_COLLAPSE_NORMALIZE_AND_ZERO)
217
+ sv_result = collect (sv. data)
218
+ @test sv_result ≈ h_sv_result
219
+ @test bitstr == [1 , 1 , 0 ]
220
+ end
221
+ end
87
222
@testset " swapIndexBits" begin
88
223
@testset for elty in [ComplexF32, ComplexF64]
89
224
# 0.1|000> + 0.4|011> - 0.4|101> - 0.3im|111>
0 commit comments