diff --git a/qiskit/algorithms/gradients/reverse_gradient/split_circuits.py b/qiskit/algorithms/gradients/reverse_gradient/split_circuits.py index 822ee5853df1..5a373756a4cf 100644 --- a/qiskit/algorithms/gradients/reverse_gradient/split_circuits.py +++ b/qiskit/algorithms/gradients/reverse_gradient/split_circuits.py @@ -35,31 +35,31 @@ def split( corresponding_parameters = [] sub = QuantumCircuit(*circuit.qregs, *circuit.cregs) - for op in circuit.data: + for inst in circuit.data: # check if new split must be created if parameters is None: params = [ param - for param in op[0].params + for param in inst.operation.params if isinstance(param, ParameterExpression) and len(param.parameters) > 0 ] else: - if op[0].definition is not None: - free_op_params = op[0].definition.parameters + if inst[0].definition is not None: + free_inst_params = inst.operation.definition.parameters else: - free_op_params = {} + free_inst_params = {} - params = [p for p in parameters if p in free_op_params] + params = [p for p in parameters if p in free_inst_params] new_split = bool(len(params) > 0) if new_split: - sub.data += [op] + sub.append(inst) circuits.append(sub) corresponding_parameters.append(params) sub = QuantumCircuit(*circuit.qregs, *circuit.cregs) else: - sub.data += [op] + sub.append(inst) # handle leftover gates if len(sub.data) > 0: diff --git a/releasenotes/notes/fix-partial-reverse-gradient-f35fb1f30ee15692.yaml b/releasenotes/notes/fix-partial-reverse-gradient-f35fb1f30ee15692.yaml new file mode 100644 index 000000000000..c2a2cc8306fb --- /dev/null +++ b/releasenotes/notes/fix-partial-reverse-gradient-f35fb1f30ee15692.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fix a performance bug in :class:`.ReverseEstimatorGradient` where the calculation + did a large amount of unnecessary copies if the gradient was only calculated for + a subset of parameters, or in a circuit with many unparameterized gates.