Skip to content

Commit

Permalink
Add support for parameterized qobj
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Apr 7, 2020
1 parent 2dbe2b4 commit e494d2c
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions src/framework/qobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace AER {
//============================================================================

class Qobj {
public:
public:
//----------------------------------------------------------------
// Constructors
//----------------------------------------------------------------
Expand All @@ -51,13 +51,12 @@ class Qobj {
json_t config; // (optional) qobj level config data
};


//============================================================================
// JSON initialization and deserialization
//============================================================================

// JSON deserialization
inline void from_json(const json_t &js, Qobj &qobj) {qobj = Qobj(js);}
inline void from_json(const json_t &js, Qobj &qobj) { qobj = Qobj(js); }

Qobj::Qobj(const json_t &js) {
// Check required fields
Expand All @@ -83,23 +82,76 @@ Qobj::Qobj(const json_t &js) {
int_t seed = -1;
uint_t seed_shift = 0;
JSON::get_value(seed, "seed_simulator", config);

// Parse experiments
const json_t &circs = js["experiments"];
for (const auto &circ : circs) {
Circuit circuit(circ, config);
// Override random seed with fixed seed if set
// We shift the seed for each successive experiment
// So that results aren't correlated between experiments
if (seed >= 0) {
const size_t num_circs = circs.size();

// Check if parameterized qobj
// It should be of the form
// [exp0_params, exp1_params, ...]
// where:
// expk_params = [((i, j), pars), ....]
// i is the instruction index in the experiment
// j is the param index in the instruction
// pars = [par0, par1, ...] is a list of different parameterizations
using pos_t = std::pair<uint_t, uint_t>;
using exp_params_t = std::vector<std::pair<pos_t, std::vector<double>>>;
std::vector<exp_params_t> param_table;
JSON::get_value(param_table, "parameterizations", config);

// Validate parameterizations for number of circuis
if (!param_table.empty() && param_table.size() != num_circs) {
throw std::invalid_argument(
R"(Invalid parameterized qobj: "parameterizations" length does not match number of circuits.)");
}

// Load circuits
for (size_t i=0; i<num_circs; i++) {
// Get base circuit from qobj
Circuit circuit(circs[i], config);
if (param_table.empty() || param_table[i].empty()) {
// Non parameterized circuit
circuits.push_back(circuit);
} else {
// Load different parameterizations of the initial circuit
const auto circ_params = param_table[i];
const size_t num_params = circ_params[0].second.size();
const size_t num_instr = circuit.ops.size();
for (size_t j=0; j<num_params; j++) {
// Make a copy of the initial circuit
Circuit param_circuit = circuit;
for (const auto &params : circ_params) {
const auto instr_pos = params.first.first;
const auto param_pos = params.first.second;
// Validation
if (instr_pos >= num_instr) {
throw std::invalid_argument(R"(Invalid parameterized qobj: instruction position out of range)");
}
auto &op = param_circuit.ops[instr_pos];
if (param_pos >= op.params.size()) {
throw std::invalid_argument(R"(Invalid parameterized qobj: instruction param position out of range)");
}
if (j >= params.second.size()) {
throw std::invalid_argument(R"(Invalid parameterized qobj: parameterization value out of range)");
}
// Update the param
op.params[param_pos] = params.second[j];
}
circuits.push_back(param_circuit);
}
}
}
// Override random seed with fixed seed if set
// We shift the seed for each successive experiment
// So that results aren't correlated between experiments
if (seed >= 0) {
for (auto& circuit : circuits) {
circuit.set_seed(seed + seed_shift);
seed_shift += 2113; // Shift the seed
seed_shift += 2113; // Shift the seed
}
circuits.push_back(circuit);
}
}

//------------------------------------------------------------------------------
} // end namespace QISKIT
} // namespace AER
//------------------------------------------------------------------------------
#endif

0 comments on commit e494d2c

Please sign in to comment.