Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run truncation after resolving parameters #1428

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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