Skip to content

Commit

Permalink
Fixed a bug in pauli truncation and add relevant test
Browse files Browse the repository at this point in the history
  • Loading branch information
gadial committed Nov 25, 2024
1 parent f4626e0 commit 92592ab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,19 @@ 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) {
// map each qubit to its location in the pauli strings
std::unordered_map<uint_t, uint_t> ops_pauli_qubit_map;
for (size_t i = 0; i < op.qubits.size(); ++i) {
ops_pauli_qubit_map[op.qubits[i]] = op.qubits.size() - 1 - i;
}
int_t nparams = op.expval_params.size();
for (int_t i = 0; i < nparams; 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] =
pauli[pauli.size() - 1 - q->first];
pauli[ops_pauli_qubit_map[q->first]];
}
pauli = new_pauli;
}
Expand Down
18 changes: 18 additions & 0 deletions test/terra/backends/aer_simulator/test_save_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,21 @@ def test_save_expval_var_stabilizer_pauli_cache_blocking(self, method, device, p
blocking_qubits=2,
max_parallel_threads=1,
)

@supported_methods(
[
"automatic",
"stabilizer",
"statevector",
"density_matrix",
"matrix_product_state",
"tensor_network",
],
)
def test_save_expval_truncation(self, method, device):
"""Test save expval when qubits are not in use and are truncated"""
circ = QuantumCircuit(3)
circ.x(1)
oper = qi.Pauli("ZZ")
qubits = [1, 2] # qubit 0 will be truncated
self._test_save_expval(circ, oper, qubits, False, method=method, device=device)

0 comments on commit 92592ab

Please sign in to comment.