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

Fix wrong set of parameters in circuits with barriers #1775

Merged
merged 3 commits into from
Apr 12, 2023
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
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()