Skip to content

Commit

Permalink
Fix performance of ReverseEstimatorGradient for partial gradients (
Browse files Browse the repository at this point in the history
…#9491) (#9492)

* Use append over +=

* add reno

(cherry picked from commit 729f7d1)

Co-authored-by: Julien Gacon <[email protected]>
  • Loading branch information
mergify[bot] and Cryoris authored Jan 31, 2023
1 parent 61e5618 commit aaf9975
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 8 additions & 8 deletions qiskit/algorithms/gradients/reverse_gradient/split_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit aaf9975

Please sign in to comment.