Skip to content

Commit

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

* Use append over +=

* add reno
  • Loading branch information
Cryoris authored Jan 31, 2023
1 parent 51abdc3 commit 84ac275
Showing 1 changed file with 8 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

0 comments on commit 84ac275

Please sign in to comment.