Skip to content

Commit 3a5855d

Browse files
Qsim upgrade (#445)
* Upgrade qsim 0.3.1 -> 0.6.0. * removed benchmarking includes. * Update tfq_inner_product.cc a feedback. * re-trigger CI.
1 parent 1576b72 commit 3a5855d

12 files changed

+160
-190
lines changed

WORKSPACE

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ http_archive(
6767

6868
http_archive(
6969
name = "qsim",
70-
sha256 = "7e5fe6c909d0007488f910d57ed765729133437f5c5f88085fa6deb544cb97dc",
71-
strip_prefix = "qsim-0.3.1",
72-
urls = ["https://github.com/quantumlib/qsim/archive/v0.3.1.zip"],
70+
sha256 = "f390ee72cf88c48d81c98262c599dc45d660a2a9308a9ee903bfa73aec08a9b4",
71+
strip_prefix = "qsim-0.6.0",
72+
urls = ["https://github.com/quantumlib/qsim/archive/v0.6.0.zip"],
7373
)
7474

7575
# Added for crosstool in tensorflow.

tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,24 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
155155
const auto tfq_for = tfq::QsimFor(context);
156156
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
157157
using StateSpace = Simulator::StateSpace;
158-
using State = StateSpace::State;
159158

160159
// Begin simulation.
161160
int largest_nq = 1;
162-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
163-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
161+
Simulator sim = Simulator(tfq_for);
162+
StateSpace ss = StateSpace(tfq_for);
163+
auto sv = ss.Create(largest_nq);
164+
auto scratch = ss.Create(largest_nq);
164165

165166
// Simulate programs one by one. Parallelizing over state vectors
166167
// we no longer parallelize over circuits. Each time we encounter a
167168
// a larger circuit we will grow the Statevector as necessary.
168169
for (int i = 0; i < fused_circuits.size(); i++) {
169170
int nq = num_qubits[i];
170-
Simulator sim = Simulator(nq, tfq_for);
171-
StateSpace ss = StateSpace(nq, tfq_for);
172171
if (nq > largest_nq) {
173172
// need to switch to larger statespace.
174173
largest_nq = nq;
175-
sv = ss.CreateState();
176-
scratch = ss.CreateState();
174+
sv = ss.Create(largest_nq);
175+
scratch = ss.Create(largest_nq);
177176
}
178177
// TODO: add heuristic here so that we do not always recompute
179178
// the state if there is a possibility that circuit[i] and
@@ -211,7 +210,6 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
211210
const auto tfq_for = qsim::SequentialFor(1);
212211
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
213212
using StateSpace = Simulator::StateSpace;
214-
using State = StateSpace::State;
215213

216214
const int output_dim_internal_size = output_tensor->dimension(1);
217215

@@ -221,15 +219,15 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
221219
int largest_nq = 1;
222220
int cur_internal_index;
223221

224-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
225-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
222+
Simulator sim = Simulator(tfq_for);
223+
StateSpace ss = StateSpace(tfq_for);
224+
auto sv = ss.Create(largest_nq);
225+
auto scratch = ss.Create(largest_nq);
226226
for (int i = start; i < end; i++) {
227227
cur_batch_index = i / output_dim_internal_size;
228228
cur_internal_index = i % output_dim_internal_size;
229229

230230
const int nq = num_qubits[cur_batch_index];
231-
Simulator sim = Simulator(nq, tfq_for);
232-
StateSpace ss = StateSpace(nq, tfq_for);
233231

234232
// (#679) Just ignore empty program
235233
if (fused_circuits[cur_batch_index].size() == 0) {
@@ -242,9 +240,9 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
242240
// We've run into a new state vector we must compute.
243241
// Only compute a new state vector when we have to.
244242
if (nq > largest_nq) {
245-
sv = ss.CreateState();
246-
scratch = ss.CreateState();
247243
largest_nq = nq;
244+
sv = ss.Create(largest_nq);
245+
scratch = ss.Create(largest_nq);
248246
}
249247
// no need to update scratch_state since ComputeExpectation
250248
// will take care of things for us.

tensorflow_quantum/core/ops/tfq_adj_grad_op.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,24 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
172172
const auto tfq_for = qsim::SequentialFor(1);
173173
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
174174
using StateSpace = Simulator::StateSpace;
175-
using State = StateSpace::State;
176175

177176
auto DoWork = [&](int start, int end) {
178177
// Begin simulation.
179178
int largest_nq = 1;
180-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
181-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
182-
State scratch2 = StateSpace(largest_nq, tfq_for).CreateState();
179+
Simulator sim = Simulator(tfq_for);
180+
StateSpace ss = StateSpace(tfq_for);
181+
auto sv = ss.Create(largest_nq);
182+
auto scratch = ss.Create(largest_nq);
183+
auto scratch2 = ss.Create(largest_nq);
183184

184185
for (int i = start; i < end; i++) {
185186
int nq = num_qubits[i];
186-
Simulator sim = Simulator(nq, tfq_for);
187-
StateSpace ss = StateSpace(nq, tfq_for);
188187
if (nq > largest_nq) {
189188
// need to switch to larger statespace.
190189
largest_nq = nq;
191-
sv = ss.CreateState();
192-
scratch = ss.CreateState();
193-
scratch2 = ss.CreateState();
190+
sv = ss.Create(largest_nq);
191+
scratch = ss.Create(largest_nq);
192+
scratch2 = ss.Create(largest_nq);
194193
}
195194

196195
// (#679) Just ignore empty program
@@ -225,7 +224,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
225224
for (int k = 0; k < gradient_gates[i][j - 1].grad_gates.size(); k++) {
226225
// Copy sv onto scratch2 in anticipation of non-unitary "gradient
227226
// gate".
228-
ss.CopyState(sv, scratch2);
227+
ss.Copy(sv, scratch2);
229228
qsim::ApplyGate(sim, gradient_gates[i][j - 1].grad_gates[k],
230229
scratch2);
231230

@@ -269,24 +268,24 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
269268
const auto tfq_for = tfq::QsimFor(context);
270269
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
271270
using StateSpace = Simulator::StateSpace;
272-
using State = StateSpace::State;
273271

274272
// Begin simulation.
275273
int largest_nq = 1;
276-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
277-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
278-
State scratch2 = StateSpace(largest_nq, tfq_for).CreateState();
274+
Simulator sim = Simulator(tfq_for);
275+
StateSpace ss = StateSpace(tfq_for);
276+
auto sv = ss.Create(largest_nq);
277+
auto scratch = ss.Create(largest_nq);
278+
auto scratch2 = ss.Create(largest_nq);
279279

280280
for (int i = 0; i < partial_fused_circuits.size(); i++) {
281281
int nq = num_qubits[i];
282-
Simulator sim = Simulator(nq, tfq_for);
283-
StateSpace ss = StateSpace(nq, tfq_for);
282+
284283
if (nq > largest_nq) {
285284
// need to switch to larger statespace.
286285
largest_nq = nq;
287-
sv = ss.CreateState();
288-
scratch = ss.CreateState();
289-
scratch2 = ss.CreateState();
286+
sv = ss.Create(largest_nq);
287+
scratch = ss.Create(largest_nq);
288+
scratch2 = ss.Create(largest_nq);
290289
}
291290

292291
// (#679) Just ignore empty program
@@ -321,7 +320,7 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
321320
for (int k = 0; k < gradient_gates[i][j - 1].grad_gates.size(); k++) {
322321
// Copy sv onto scratch2 in anticipation of non-unitary "gradient
323322
// gate".
324-
ss.CopyState(sv, scratch2);
323+
ss.Copy(sv, scratch2);
325324
qsim::ApplyGate(sim, gradient_gates[i][j - 1].grad_gates[k],
326325
scratch2);
327326

tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,25 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
127127
const auto tfq_for = tfq::QsimFor(context);
128128
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
129129
using StateSpace = Simulator::StateSpace;
130-
using State = StateSpace::State;
131130

132131
// Begin simulation.
133132
int largest_nq = 1;
134-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
135-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
133+
Simulator sim = Simulator(tfq_for);
134+
StateSpace ss = StateSpace(tfq_for);
135+
auto sv = ss.Create(largest_nq);
136+
auto scratch = ss.Create(largest_nq);
136137

137138
// Simulate programs one by one. Parallelizing over state vectors
138139
// we no longer parallelize over circuits. Each time we encounter a
139140
// a larger circuit we will grow the Statevector as necessary.
140141
for (int i = 0; i < fused_circuits.size(); i++) {
141142
int nq = num_qubits[i];
142-
Simulator sim = Simulator(nq, tfq_for);
143-
StateSpace ss = StateSpace(nq, tfq_for);
143+
144144
if (nq > largest_nq) {
145145
// need to switch to larger statespace.
146146
largest_nq = nq;
147-
sv = ss.CreateState();
148-
scratch = ss.CreateState();
147+
sv = ss.Create(largest_nq);
148+
scratch = ss.Create(largest_nq);
149149
}
150150
// TODO: add heuristic here so that we do not always recompute
151151
// the state if there is a possibility that circuit[i] and
@@ -178,7 +178,6 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
178178
const auto tfq_for = qsim::SequentialFor(1);
179179
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
180180
using StateSpace = Simulator::StateSpace;
181-
using State = StateSpace::State;
182181

183182
const int output_dim_op_size = output_tensor->dimension(1);
184183

@@ -188,15 +187,15 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
188187
int largest_nq = 1;
189188
int cur_op_index;
190189

191-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
192-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
190+
Simulator sim = Simulator(tfq_for);
191+
StateSpace ss = StateSpace(tfq_for);
192+
auto sv = ss.Create(largest_nq);
193+
auto scratch = ss.Create(largest_nq);
193194
for (int i = start; i < end; i++) {
194195
cur_batch_index = i / output_dim_op_size;
195196
cur_op_index = i % output_dim_op_size;
196197

197198
const int nq = num_qubits[cur_batch_index];
198-
Simulator sim = Simulator(nq, tfq_for);
199-
StateSpace ss = StateSpace(nq, tfq_for);
200199

201200
// (#679) Just ignore empty program
202201
if (fused_circuits[cur_batch_index].size() == 0) {
@@ -208,9 +207,9 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
208207
// We've run into a new state vector we must compute.
209208
// Only compute a new state vector when we have to.
210209
if (nq > largest_nq) {
211-
sv = ss.CreateState();
212-
scratch = ss.CreateState();
213210
largest_nq = nq;
211+
sv = ss.Create(largest_nq);
212+
scratch = ss.Create(largest_nq);
214213
}
215214
// no need to update scratch_state since ComputeExpectation
216215
// will take care of things for us.

tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,25 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
144144
const auto tfq_for = tfq::QsimFor(context);
145145
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
146146
using StateSpace = Simulator::StateSpace;
147-
using State = StateSpace::State;
148147

149148
// Begin simulation.
150149
int largest_nq = 1;
151-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
152-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
150+
Simulator sim = Simulator(tfq_for);
151+
StateSpace ss = StateSpace(tfq_for);
152+
auto sv = ss.Create(largest_nq);
153+
auto scratch = ss.Create(largest_nq);
153154

154155
// Simulate programs one by one. Parallelizing over state vectors
155156
// we no longer parallelize over circuits. Each time we encounter a
156157
// a larger circuit we will grow the Statevector as necessary.
157158
for (int i = 0; i < fused_circuits.size(); i++) {
158159
int nq = num_qubits[i];
159-
Simulator sim = Simulator(nq, tfq_for);
160-
StateSpace ss = StateSpace(nq, tfq_for);
160+
161161
if (nq > largest_nq) {
162162
// need to switch to larger statespace.
163163
largest_nq = nq;
164-
sv = ss.CreateState();
165-
scratch = ss.CreateState();
164+
sv = ss.Create(largest_nq);
165+
scratch = ss.Create(largest_nq);
166166
}
167167
// TODO: add heuristic here so that we do not always recompute
168168
// the state if there is a possibility that circuit[i] and
@@ -196,7 +196,6 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
196196
const auto tfq_for = qsim::SequentialFor(1);
197197
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
198198
using StateSpace = Simulator::StateSpace;
199-
using State = StateSpace::State;
200199

201200
const int output_dim_op_size = output_tensor->dimension(1);
202201

@@ -206,15 +205,15 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
206205
int largest_nq = 1;
207206
int cur_op_index;
208207

209-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
210-
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
208+
Simulator sim = Simulator(tfq_for);
209+
StateSpace ss = StateSpace(tfq_for);
210+
auto sv = ss.Create(largest_nq);
211+
auto scratch = ss.Create(largest_nq);
211212
for (int i = start; i < end; i++) {
212213
cur_batch_index = i / output_dim_op_size;
213214
cur_op_index = i % output_dim_op_size;
214215

215216
const int nq = num_qubits[cur_batch_index];
216-
Simulator sim = Simulator(nq, tfq_for);
217-
StateSpace ss = StateSpace(nq, tfq_for);
218217

219218
// (#679) Just ignore empty program
220219
if (fused_circuits[cur_batch_index].size() == 0) {
@@ -226,9 +225,9 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
226225
// We've run into a new state vector we must compute.
227226
// Only compute a new state vector when we have to.
228227
if (nq > largest_nq) {
229-
sv = ss.CreateState();
230-
scratch = ss.CreateState();
231228
largest_nq = nq;
229+
sv = ss.Create(largest_nq);
230+
scratch = ss.Create(largest_nq);
232231
}
233232
// no need to update scratch_state since ComputeExpectation
234233
// will take care of things for us.

tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,23 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel {
127127
const auto tfq_for = tfq::QsimFor(context);
128128
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
129129
using StateSpace = Simulator::StateSpace;
130-
using State = StateSpace::State;
131130

132131
// Begin simulation.
133132
int largest_nq = 1;
134-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
133+
Simulator sim = Simulator(tfq_for);
134+
StateSpace ss = StateSpace(tfq_for);
135+
auto sv = ss.Create(largest_nq);
135136

136137
// Simulate programs one by one. Parallelizing over state vectors
137138
// we no longer parallelize over circuits. Each time we encounter a
138139
// a larger circuit we will grow the Statevector as nescessary.
139140
for (int i = 0; i < fused_circuits.size(); i++) {
140141
int nq = num_qubits[i];
141-
Simulator sim = Simulator(nq, tfq_for);
142-
StateSpace ss = StateSpace(nq, tfq_for);
142+
143143
if (nq > largest_nq) {
144144
// need to switch to larger statespace.
145145
largest_nq = nq;
146-
sv = ss.CreateState();
146+
sv = ss.Create(largest_nq);
147147
}
148148
ss.SetStateZero(sv);
149149
for (int j = 0; j < fused_circuits[i].size(); j++) {
@@ -180,19 +180,19 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel {
180180
const auto tfq_for = qsim::SequentialFor(1);
181181
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
182182
using StateSpace = Simulator::StateSpace;
183-
using State = StateSpace::State;
184183

185184
auto DoWork = [&](int start, int end) {
186185
int largest_nq = 1;
187-
State sv = StateSpace(largest_nq, tfq_for).CreateState();
186+
Simulator sim = Simulator(tfq_for);
187+
StateSpace ss = StateSpace(tfq_for);
188+
auto sv = ss.Create(largest_nq);
188189
for (int i = start; i < end; i++) {
189190
int nq = num_qubits[i];
190-
Simulator sim = Simulator(nq, tfq_for);
191-
StateSpace ss = StateSpace(nq, tfq_for);
191+
192192
if (nq > largest_nq) {
193193
// need to switch to larger statespace.
194194
largest_nq = nq;
195-
sv = ss.CreateState();
195+
sv = ss.Create(largest_nq);
196196
}
197197
ss.SetStateZero(sv);
198198
for (int j = 0; j < fused_circuits[i].size(); j++) {

0 commit comments

Comments
 (0)