Skip to content

Commit

Permalink
Fix wrong set of parameters in circuits with barriers (Qiskit#1775)
Browse files Browse the repository at this point in the history
* Fix wrong set of parameters in circuits with barriers

`AerCircuit` is created from a circuit by iterating its instrucitons
while skpping barrier. This leads inconsistency with positions of
parameter bindings. This commit adds barrier instruction to the class
and keeps positions of parameter bindings.

* fix lint error in test

* remove unused variable in test
  • Loading branch information
hhorii committed Apr 20, 2023
1 parent f75f0f6 commit 9348aad
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def _assemble_op(aer_circ, inst, qubit_indices, clbit_indices, is_conditional, c
elif name == "superop":
aer_circ.superop(qubits, params[0], conditional_reg)
elif name == "barrier":
pass
aer_circ.barrier(qubits)
elif name == "jump":
aer_circ.jump(qubits, params, conditional_reg)
elif name == "mark":
Expand Down
1 change: 1 addition & 0 deletions qiskit_aer/backends/wrappers/aer_circuit_binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void bind_aer_circuit(MODULE m) {
aer_circuit.def("set_clifford", &Circuit::set_clifford<py::handle>);
aer_circuit.def("jump", &Circuit::jump);
aer_circuit.def("mark", &Circuit::mark);
aer_circuit.def("barrier", &Circuit::barrier);
aer_circuit.def("measure", &Circuit::measure);
aer_circuit.def("reset", &Circuit::reset);
aer_circuit.def("set_qerror_loc", &Circuit::set_qerror_loc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
:class:`~.AerCircuit` is created from a circuit by iterating its operations
while skipping barrier instructions. However, skipping barrier instructions
make wrong positionings of parameter bindings. This fix adds
:meth:`~.AerCircuit.barrier` and keeps parametr bindings correct.
4 changes: 4 additions & 0 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ class Circuit {
ops.push_back(Operations::make_mark(qubits, params));
}

void barrier(const reg_t &qubits) {
ops.push_back(Operations::make_barrier(qubits));
}

void measure(const reg_t &qubits, const reg_t &memory,
const reg_t &registers) {
ops.push_back(Operations::make_measure(qubits, memory, registers));
Expand Down
8 changes: 8 additions & 0 deletions src/framework/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,14 @@ inline Op make_mark(const reg_t &qubits,
return op;
}

inline Op make_barrier(const reg_t &qubits) {
Op op;
op.type = OpType::barrier;
op.name = "barrier";
op.qubits = qubits;
return op;
}

inline Op make_measure(const reg_t &qubits, const reg_t &memory,
const reg_t &registers) {
Op op;
Expand Down
22 changes: 22 additions & 0 deletions test/terra/backends/test_parameterized_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,28 @@ def test_run_empty(self):
with self.assertRaises(AerError):
res = backend.run(circuit, shots=shots, parameter_binds=parameter_binds).result()

def test_parameters_with_barrier(self):
"""Test parameterized circuit path with barrier"""
backend = AerSimulator()
circuit = QuantumCircuit(3)
theta = Parameter("theta")
phi = Parameter("phi")
circuit.rx(theta, 0)
circuit.rx(theta, 1)
circuit.rx(theta, 2)
circuit.barrier()
circuit.rx(phi, 0)
circuit.rx(phi, 1)
circuit.rx(phi, 2)
circuit.barrier()
circuit.measure_all()

parameter_binds = [{theta: [pi / 2], phi: [pi / 2]}]
res = backend.run([circuit], shots=1024, parameter_binds=parameter_binds).result()

self.assertSuccess(res)
self.assertEqual(res.get_counts(), {"111": 1024})


if __name__ == "__main__":
unittest.main()

0 comments on commit 9348aad

Please sign in to comment.