Skip to content

Commit

Permalink
add num_original_qubits to aer_circuit to get num_qubits without anci…
Browse files Browse the repository at this point in the history
…lla qubits
  • Loading branch information
doichanj committed Sep 10, 2024
1 parent 41322a7 commit 51dadef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ def assemble_circuit(circuit: QuantumCircuit, basis_gates=None):
aer_circ = AerCircuit()
aer_circ.set_header(header)
aer_circ.num_qubits = num_qubits
aer_circ.num_original_qubits = num_qubits
if hasattr(circuit, "layout"):
if hasattr(circuit.layout, "final_index_layout"):
if circuit.layout.final_index_layout(True) is not None:
aer_circ.num_original_qubits = len(circuit.layout.final_index_layout(True))
aer_circ.num_memory = num_memory
aer_circ.global_phase_angle = global_phase

Expand Down
2 changes: 2 additions & 0 deletions qiskit_aer/backends/wrappers/aer_circuit_binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ void bind_aer_circuit(MODULE m) {
aer_circuit.def_readwrite("circ_id", &Circuit::circ_id);
aer_circuit.def_readwrite("shots", &Circuit::shots);
aer_circuit.def_readwrite("num_qubits", &Circuit::num_qubits);
aer_circuit.def_readwrite("num_original_qubits",
&Circuit::num_original_qubits);
aer_circuit.def_readwrite("num_memory", &Circuit::num_memory);
aer_circuit.def_readwrite("seed", &Circuit::seed);
aer_circuit.def_readwrite("ops", &Circuit::ops);
Expand Down
15 changes: 12 additions & 3 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Circuit {
uint_t num_qubits = 0; // maximum number of qubits needed for ops
uint_t num_memory = 0; // maximum number of memory clbits needed for ops
uint_t num_registers = 0; // maximum number of registers clbits needed for ops
uint_t num_original_qubits = 0; // number of qubits without ancilla qubits

// Measurement params
bool has_conditional = false; // True if any ops are conditional
Expand Down Expand Up @@ -419,7 +420,10 @@ void Circuit::reset_metadata() {
void Circuit::add_op_metadata(const Op &op) {
has_conditional |= op.conditional;
opset_.insert(op);
if (op.type != OpType::save_expval && op.type != OpType::save_expval_var) {
if (op.qubits.size() > num_original_qubits) {
qubitset_.insert(op.qubits.begin(),
op.qubits.begin() + num_original_qubits);
} else {
qubitset_.insert(op.qubits.begin(), op.qubits.end());
}
memoryset_.insert(op.memory.begin(), op.memory.end());
Expand Down Expand Up @@ -498,7 +502,6 @@ void Circuit::set_params(bool truncation) {
}

// Set qubit and memory size
uint_t num_qubits_orig = num_qubits;
num_memory = (memoryset_.empty()) ? 0 : 1 + *memoryset_.rbegin();
num_registers = (registerset_.empty()) ? 0 : 1 + *registerset_.rbegin();
if (remapped_qubits) {
Expand Down Expand Up @@ -592,14 +595,17 @@ void Circuit::set_params(bool truncation) {
}
if (remapped_qubits) {
remap_qubits(ops[pos]);
} else if (truncation && qubitmap_.size() != num_qubits_orig) {
} else if (truncation && qubitmap_.size() < ops[pos].qubits.size()) {
// 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].expval_params.size();
for (int_t i = 0; i < nparams; i++) {
std::string &pauli = std::get<0>(ops[pos].expval_params[i]);
std::cout << " before truncate : " << pauli << std::endl;
pauli.assign(pauli.end() - qubitmap_.size(), pauli.end());
std::cout << " after truncate : "
<< std::get<0>(ops[pos].expval_params[i]) << std::endl;
}
ops[pos].qubits.resize(qubitmap_.size());
}
Expand Down Expand Up @@ -673,12 +679,15 @@ void Circuit::remap_qubits(Op &op) const {
for (int_t i = 0; i < nparams; i++) {
std::string &pauli = std::get<0>(op.expval_params[i]);
std::string new_pauli;
std::cout << " before remap : " << pauli << std::endl;
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 = new_pauli;
std::cout << " after remap : " << std::get<0>(op.expval_params[i])
<< std::endl;
}
for (int_t i = 0; i < qubitmap_.size(); i++) {
op.qubits[i] = i;
Expand Down
1 change: 1 addition & 0 deletions test/terra/backends/aer_simulator/test_save_expval.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from qiskit import QuantumCircuit
from qiskit.circuit.library import QuantumVolume
from qiskit.compiler import transpile
from qiskit.providers.fake_provider import GenericBackendV2

PAULI2 = [
"II",
Expand Down

0 comments on commit 51dadef

Please sign in to comment.