Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ http_archive(

http_archive(
name = "qsim",
sha256 = "7e5fe6c909d0007488f910d57ed765729133437f5c5f88085fa6deb544cb97dc",
strip_prefix = "qsim-0.3.1",
urls = ["https://github.com/quantumlib/qsim/archive/v0.3.1.zip"],
sha256 = "f390ee72cf88c48d81c98262c599dc45d660a2a9308a9ee903bfa73aec08a9b4",
strip_prefix = "qsim-0.6.0",
urls = ["https://github.com/quantumlib/qsim/archive/v0.6.0.zip"],
)

# Added for crosstool in tensorflow.
Expand Down
26 changes: 12 additions & 14 deletions tensorflow_quantum/core/ops/math_ops/tfq_inner_product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,24 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
const auto tfq_for = tfq::QsimFor(context);
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);

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

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

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

State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);
for (int i = start; i < end; i++) {
cur_batch_index = i / output_dim_internal_size;
cur_internal_index = i % output_dim_internal_size;

const int nq = num_qubits[cur_batch_index];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

// (#679) Just ignore empty program
if (fused_circuits[cur_batch_index].size() == 0) {
Expand All @@ -242,9 +240,9 @@ class TfqInnerProductOp : public tensorflow::OpKernel {
// We've run into a new state vector we must compute.
// Only compute a new state vector when we have to.
if (nq > largest_nq) {
sv = ss.CreateState();
scratch = ss.CreateState();
largest_nq = nq;
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
}
// no need to update scratch_state since ComputeExpectation
// will take care of things for us.
Expand Down
39 changes: 19 additions & 20 deletions tensorflow_quantum/core/ops/tfq_adj_grad_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,24 @@ class TfqAdjointGradientOp : public tensorflow::OpKernel {
const auto tfq_for = qsim::SequentialFor(1);
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

auto DoWork = [&](int start, int end) {
// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
State scratch2 = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);
auto scratch2 = ss.Create(largest_nq);

for (int i = start; i < end; i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);
if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
scratch = ss.CreateState();
scratch2 = ss.CreateState();
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
scratch2 = ss.Create(largest_nq);
}

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

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

// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
State scratch2 = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);
auto scratch2 = ss.Create(largest_nq);

for (int i = 0; i < partial_fused_circuits.size(); i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
scratch = ss.CreateState();
scratch2 = ss.CreateState();
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
scratch2 = ss.Create(largest_nq);
}

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

Expand Down
27 changes: 13 additions & 14 deletions tensorflow_quantum/core/ops/tfq_simulate_expectation_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
const auto tfq_for = tfq::QsimFor(context);
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);

// Simulate programs one by one. Parallelizing over state vectors
// we no longer parallelize over circuits. Each time we encounter a
// a larger circuit we will grow the Statevector as necessary.
for (int i = 0; i < fused_circuits.size(); i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
scratch = ss.CreateState();
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
}
// TODO: add heuristic here so that we do not always recompute
// the state if there is a possibility that circuit[i] and
Expand Down Expand Up @@ -178,7 +178,6 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
const auto tfq_for = qsim::SequentialFor(1);
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

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

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

State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);
for (int i = start; i < end; i++) {
cur_batch_index = i / output_dim_op_size;
cur_op_index = i % output_dim_op_size;

const int nq = num_qubits[cur_batch_index];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

// (#679) Just ignore empty program
if (fused_circuits[cur_batch_index].size() == 0) {
Expand All @@ -208,9 +207,9 @@ class TfqSimulateExpectationOp : public tensorflow::OpKernel {
// We've run into a new state vector we must compute.
// Only compute a new state vector when we have to.
if (nq > largest_nq) {
sv = ss.CreateState();
scratch = ss.CreateState();
largest_nq = nq;
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
}
// no need to update scratch_state since ComputeExpectation
// will take care of things for us.
Expand Down
27 changes: 13 additions & 14 deletions tensorflow_quantum/core/ops/tfq_simulate_sampled_expectation_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,25 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
const auto tfq_for = tfq::QsimFor(context);
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);

// Simulate programs one by one. Parallelizing over state vectors
// we no longer parallelize over circuits. Each time we encounter a
// a larger circuit we will grow the Statevector as necessary.
for (int i = 0; i < fused_circuits.size(); i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
scratch = ss.CreateState();
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
}
// TODO: add heuristic here so that we do not always recompute
// the state if there is a possibility that circuit[i] and
Expand Down Expand Up @@ -196,7 +196,6 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
const auto tfq_for = qsim::SequentialFor(1);
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

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

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

State sv = StateSpace(largest_nq, tfq_for).CreateState();
State scratch = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
auto scratch = ss.Create(largest_nq);
for (int i = start; i < end; i++) {
cur_batch_index = i / output_dim_op_size;
cur_op_index = i % output_dim_op_size;

const int nq = num_qubits[cur_batch_index];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

// (#679) Just ignore empty program
if (fused_circuits[cur_batch_index].size() == 0) {
Expand All @@ -226,9 +225,9 @@ class TfqSimulateSampledExpectationOp : public tensorflow::OpKernel {
// We've run into a new state vector we must compute.
// Only compute a new state vector when we have to.
if (nq > largest_nq) {
sv = ss.CreateState();
scratch = ss.CreateState();
largest_nq = nq;
sv = ss.Create(largest_nq);
scratch = ss.Create(largest_nq);
}
// no need to update scratch_state since ComputeExpectation
// will take care of things for us.
Expand Down
20 changes: 10 additions & 10 deletions tensorflow_quantum/core/ops/tfq_simulate_samples_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel {
const auto tfq_for = tfq::QsimFor(context);
using Simulator = qsim::Simulator<const tfq::QsimFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

// Begin simulation.
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);

// Simulate programs one by one. Parallelizing over state vectors
// we no longer parallelize over circuits. Each time we encounter a
// a larger circuit we will grow the Statevector as nescessary.
for (int i = 0; i < fused_circuits.size(); i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
sv = ss.Create(largest_nq);
}
ss.SetStateZero(sv);
for (int j = 0; j < fused_circuits[i].size(); j++) {
Expand Down Expand Up @@ -180,19 +180,19 @@ class TfqSimulateSamplesOp : public tensorflow::OpKernel {
const auto tfq_for = qsim::SequentialFor(1);
using Simulator = qsim::Simulator<const qsim::SequentialFor&>;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;

auto DoWork = [&](int start, int end) {
int largest_nq = 1;
State sv = StateSpace(largest_nq, tfq_for).CreateState();
Simulator sim = Simulator(tfq_for);
StateSpace ss = StateSpace(tfq_for);
auto sv = ss.Create(largest_nq);
for (int i = start; i < end; i++) {
int nq = num_qubits[i];
Simulator sim = Simulator(nq, tfq_for);
StateSpace ss = StateSpace(nq, tfq_for);

if (nq > largest_nq) {
// need to switch to larger statespace.
largest_nq = nq;
sv = ss.CreateState();
sv = ss.Create(largest_nq);
}
ss.SetStateZero(sv);
for (int j = 0; j < fused_circuits[i].size(); j++) {
Expand Down
Loading