Skip to content

Commit

Permalink
fix truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
doichanj committed Sep 9, 2024
1 parent 17b5b01 commit 41322a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
21 changes: 7 additions & 14 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,10 @@ void Circuit::set_params(bool truncation) {
// truncate save_expval here when remap is not needed
if (ops[pos].type == OpType::save_expval ||
ops[pos].type == OpType::save_expval_var) {
int_t nparams = ops[pos].string_params.size();
int_t nparams = ops[pos].expval_params.size();
for (int_t i = 0; i < nparams; i++) {
// save original pauli string to restore for output
ops[pos].string_params.push_back(ops[pos].string_params[i]);

ops[pos].string_params[i].assign(ops[pos].string_params[i].end() -
qubitmap_.size(),
ops[pos].string_params[i].end());
std::string &pauli = std::get<0>(ops[pos].expval_params[i]);
pauli.assign(pauli.end() - qubitmap_.size(), pauli.end());
}
ops[pos].qubits.resize(qubitmap_.size());
}
Expand Down Expand Up @@ -673,19 +669,16 @@ void Circuit::set_params(bool truncation) {
void Circuit::remap_qubits(Op &op) const {
// truncate save_expval
if (op.type == OpType::save_expval || op.type == OpType::save_expval_var) {
int_t nparams = op.string_params.size();
int_t nparams = op.expval_params.size();
for (int_t i = 0; i < nparams; i++) {
uint_t size = op.string_params[i].length();
// save original pauli string to restore for output
op.string_params.push_back(op.string_params[i]);
std::string &pauli = std::get<0>(op.expval_params[i]);
std::string new_pauli;
new_pauli.resize(qubitmap_.size());

for (auto q = qubitmap_.cbegin(); q != qubitmap_.cend(); q++) {
new_pauli[qubitmap_.size() - 1 - q->second] =
op.string_params[i][size - 1 - q->first];
pauli[pauli.size() - 1 - q->first];
}
op.string_params[i] = std::move(new_pauli);
pauli = new_pauli;
}
for (int_t i = 0; i < qubitmap_.size(); i++) {
op.qubits[i] = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,17 +803,14 @@ void State::apply_save_expval(const Operations::Op &op,
sq_expval += std::get<2>(param) * val;
}
}
int_t key = 0;
if (op.string_params.size() > 1)
key = 1;
if (variance) {
std::vector<double> expval_var(2);
expval_var[0] = expval; // mean
expval_var[1] = sq_expval - expval * expval; // variance
result.save_data_average(creg(), op.string_params[key], expval_var, op.type,
result.save_data_average(creg(), op.string_params[0], expval_var, op.type,
op.save_type);
} else {
result.save_data_average(creg(), op.string_params[key], expval, op.type,
result.save_data_average(creg(), op.string_params[0], expval, op.type,
op.save_type);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/simulators/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +434,14 @@ void Base::apply_save_expval(const Operations::Op &op,
sq_expval += std::get<2>(param) * val;
}
}
int_t key = 0;
if (op.string_params.size() > 1)
key = 1;
if (variance) {
std::vector<double> expval_var(2);
expval_var[0] = expval; // mean
expval_var[1] = sq_expval - expval * expval; // variance
result.save_data_average(creg(), op.string_params[key], expval_var, op.type,
result.save_data_average(creg(), op.string_params[0], expval_var, op.type,
op.save_type);
} else {
result.save_data_average(creg(), op.string_params[key], expval, op.type,
result.save_data_average(creg(), op.string_params[0], expval, op.type,
op.save_type);
}
}
Expand Down

0 comments on commit 41322a7

Please sign in to comment.