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 performance of ReverseEstimatorGradient for partial gradients #9491

Merged
merged 3 commits into from
Jan 31, 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
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.