Skip to content

Commit

Permalink
Run truncation after resolving parameters (Qiskit#1428)
Browse files Browse the repository at this point in the history
* run truncation after resolving parameters

Since 0.9.0, Aer reduces qubits and their instructions if they do not affect measured qubits.
On the other hand, parameterized QObj contains indices of instructions and their values.
Previously these values are set to instructions of a circuit after running truncation,
which may reduce some of instructions. Consequently, indices of parameterized QObj may address
wrong instructions in a truncated cicuit.

This fix changes the order of processing of truncation and parameterization: First, Aer
resolves paramers, and then runs truncation.

* Reword release note

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
  • Loading branch information
3 people committed Feb 9, 2022
1 parent 3474811 commit b086dc6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
Fixes a bug with truncation of circuits in parameterized Qobjs.
Previously parameters of parameterized QObj could be wrongly resolved
if unused qubits of their circuits were truncated, because indices of
the parameters were not updated after the instructions on unmeasured qubits
were removed.
See `#1427 <https://github.com/Qiskit/qiskit-aer/issues/1427>`__
for details.
12 changes: 10 additions & 2 deletions src/framework/qobj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ Qobj::Qobj(const inputdata_t &input) {

// Load circuits
for (size_t i=0; i<num_circs; i++) {
// Get base circuit from qobj
Circuit circuit(static_cast<inputdata_t>(circs[i]), config, truncation);
if (param_table.empty() || param_table[i].empty()) {
// Get base circuit from qobj
Circuit circuit(static_cast<inputdata_t>(circs[i]), config, truncation);
// Non parameterized circuit
circuits.push_back(std::move(circuit));
} else {
// Get base circuit from qobj without truncation
Circuit circuit(static_cast<inputdata_t>(circs[i]), config, false);
// Load different parameterizations of the initial circuit
const auto circ_params = param_table[i];
const size_t num_params = circ_params[0].second.size();
Expand All @@ -159,6 +161,12 @@ Qobj::Qobj(const inputdata_t &input) {
// Update the param
op.params[param_pos] = params.second[j];
}
// Run truncation.
// TODO: Truncation should be performed and parameters should be resolved after it.
// However, parameters are associated with indices of instructions, which can be changed in truncation.
// Therefore, current implementation performs truncation for each parameter set.
if (truncation)
param_circuit.set_params(true);
circuits.push_back(std::move(param_circuit));
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/terra/backends/test_parameterized_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
save_expval_circuit_parameterized,
save_expval_final_statevecs,
)
from qiskit.providers.aer.library import SaveStatevector
from qiskit.providers.aer import AerSimulator, AerError


Expand Down Expand Up @@ -295,6 +296,37 @@ def test_run_path_multiple_circuits_mismatch_length(self):
with self.assertRaises(AerError):
backend.run([circuit]*3, shots=shots, parameter_binds=[parameter_binds]).result()

def test_run_path_with_truncation(self):
"""Test parameterized circuits with truncation"""
backend = AerSimulator(method='statevector')
theta = Parameter('theta')
circuit = QuantumCircuit(5, 2)
for q in range(5):
circuit.ry(theta, q)
circuit.cx(0, 1)
circuit.cx(1, 2)
for q in range(5):
circuit.ry(theta, q)
circuit.cx(0, 1)
circuit.cx(1, 2)
circuit.append(SaveStatevector(3, label='sv', pershot=False, conditional=False), range(3))

param_map = {theta: [0.1 * i for i in range(3)]}
param_sets = [{theta: 0.1 * i} for i in range(3)]

resolved_circuits = [circuit.bind_parameters(param_set) for param_set in param_sets]

result = backend.run(circuit, parameter_binds=[param_map]).result()
self.assertSuccess(result)

result_without_parameters = backend.run(resolved_circuits).result()
self.assertSuccess(result_without_parameters)

for actual_result in result.results:
metadata = actual_result.metadata
self.assertEqual(metadata["active_input_qubits"], [q for q in range(3)])
for i in range(3):
self.assertEqual(result.data(i)['sv'], result_without_parameters.data(i)['sv'])


if __name__ == '__main__':
Expand Down

0 comments on commit b086dc6

Please sign in to comment.