@@ -26,18 +26,33 @@ using cuStateVec
2626 end
2727 @testset " applyMatrix! and expectation" begin
2828 # build a simple state and compute expectations
29- n_q = 2
3029 @testset for elty in [ComplexF32, ComplexF64]
31- H = convert (Matrix{elty}, (1 /√ 2 ). * [1 1 ; 1 - 1 ])
32- X = convert (Matrix{elty}, [0 1 ; 1 0 ])
33- Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
30+ result = (4.1 , 0.0 )
31+ h_sv = elty[0.0 , 0.1 * im, 0.1 + 0.1im , 0.1 + 0.2im , 0.2 + 0.2im , 0.3 + 0.3im , 0.3 + 0.4im , 0.4 + 0.5im ]
32+ O = elty[1 2 + im; 2 - im 3 ]
33+ n_q = 3
34+ sv = CuStateVec (elty, n_q)
35+ copyto! (sv. data, h_sv)
36+ exp_res = expectation (sv, O, Int32[1 ])
37+ synchronize ()
38+ @test exp_res[1 ][] ≈ result[1 ] atol= 1e-6
39+ @test exp_res[2 ][] ≈ result[2 ]
40+
41+ n_q = 2
42+ sv = CuStateVec (elty, n_q)
43+ H = convert (Matrix{elty}, (1 /√ 2 ). * [1 1 ; 1 - 1 ])
44+ X = convert (Matrix{elty}, [0 1 ; 1 0 ])
45+ Z = convert (Matrix{elty}, [1 0 ; 0 - 1 ])
3446 sv = CuStateVec (elty, n_q)
35- sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
36- sv = applyMatrix! (sv, H, false , Int32[1 ], Int32[])
37- exp, res = expectation (sv, Z, Int32[1 ])
38- @test exp ≈ 0.0 atol= 1e-6
39- exp, res = expectation (sv, X, Int32[0 ])
40- @test exp ≈ 1.0 atol= 1e-6
47+ sv = applyMatrix! (sv, X, false , Int32[0 ], Int32[])
48+ sv = applyMatrix! (sv, X, false , Int32[1 ], Int32[])
49+ exp, res = expectation (sv, CuMatrix (Z), Int32[0 ])
50+ synchronize ()
51+ @test exp[] ≈ - 1.0 atol= 1e-6
52+ exp, res = expectation (sv, CuMatrix (Z), Int32[1 ])
53+ @test exp[] ≈ - 1.0 atol= 1e-6
54+ exp, res = expectation (sv, CuMatrix (X), Int32[0 ])
55+ @test exp[] ≈ 0.0 atol= 1e-6
4156 end
4257 # build a simple state with controls and compute expectations
4358 n_q = 2
@@ -49,9 +64,11 @@ using cuStateVec
4964 sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
5065 sv = applyMatrix! (sv, X, false , Int32[1 ], Int32[0 ]) # CNOT
5166 exp, res = expectation (sv, Z, Int32[0 ])
52- @test exp ≈ 0.0 atol= 1e-6
67+ synchronize ()
68+ @test exp[] ≈ 0.0 atol= 1e-6
5369 exp, res = expectation (sv, X, Int32[0 ])
54- @test exp ≈ 0.0 atol= 1e-6
70+ synchronize ()
71+ @test exp[] ≈ 0.0 atol= 1e-6
5572 end
5673 # with expectationsOnPauliBasis
5774 n_q = 2
@@ -89,22 +106,26 @@ using cuStateVec
89106 (cuStateVec. CUSTATEVEC_MATRIX_MAP_TYPE_MATRIX_INDEXED, fill (0 , n_svs), 1 ),
90107 (cuStateVec. CUSTATEVEC_MATRIX_MAP_TYPE_BROADCAST, fill (0 , n_svs), 1 ),
91108 )
92- batched_vec = CUDA . zeros (elty, n_svs* 2 ^ (n_q))
109+ batched_vec = zeros (elty, n_svs* 2 ^ (n_q))
93110 for sv_ix in 0 : n_svs- 1
94- CUDA . @allowscalar batched_vec[sv_ix* (2 ^ n_q) + 1 ] = one (elty)
111+ batched_vec[sv_ix* (2 ^ n_q) + 1 ] = one (elty)
95112 end
96- sv = CuStateVec (batched_vec) # padded state vector
113+ sv = CuStateVec (elty, n_svs * n_q) # padded state vector
114+ copyto! (sv. data, batched_vec)
97115 H_batch = CuVector {elty} (repeat (vec (H), n_mats))
98116 sv = applyMatrixBatched! (sv, n_svs, mapping, mat_inds, H_batch, n_mats, false , Int32[0 ], Int32[])
99117 CUDA. @allowscalar begin
100118 for sv_ix in 0 : n_svs- 1
101119 ix_begin = sv_ix* 2 ^ n_q + 1
102120 ix_end = (sv_ix+ 1 )* 2 ^ n_q
103- sv_ = CuStateVec (sv. data[ix_begin: ix_end])
121+ sv_ = CuStateVec (elty, n_q)
122+ sv_. data .= sv. data[ix_begin: ix_end]
104123 exp, res = expectation (sv_, Z, Int32[0 ])
105- @test exp ≈ 0.0 atol= 1e-6
124+ synchronize ()
125+ @test exp[] ≈ 0.0 atol= 1e-6
106126 exp, res = expectation (sv_, X, Int32[0 ])
107- @test exp ≈ 1.0 atol= 1e-6
127+ synchronize ()
128+ @test exp[] ≈ 1.0 atol= 1e-6
108129 end
109130 end
110131 end
@@ -120,9 +141,11 @@ using cuStateVec
120141 sv = applyMatrix! (sv, H, false , Int32[0 ], Int32[])
121142 sv = applyMatrix! (sv, X, false , Int32[1 ], Int32[0 ]) # CNOT
122143 exp, res = expectation (sv, Z, Int32[0 ])
123- @test exp ≈ 0.0 atol= 1e-6
144+ synchronize ()
145+ @test exp[] ≈ 0.0 atol= 1e-6
124146 exp, res = expectation (sv, X, Int32[0 ])
125- @test exp ≈ 0.0 atol= 1e-6
147+ synchronize ()
148+ @test exp[] ≈ 0.0 atol= 1e-6
126149 end
127150 end
128151 @testset " applyMatrix! and sample" begin
0 commit comments