Skip to content

Commit

Permalink
Merge pull request Qiskit#1 from Cryoris/gradients-unrolling
Browse files Browse the repository at this point in the history
Fix unrolling
  • Loading branch information
dlasecki authored Aug 16, 2021
2 parents ce2623e + 916ea81 commit c0c74f6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 8 additions & 3 deletions qiskit/opflow/gradients/circuit_gradients/lin_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class LinComb(CircuitGradient):
see e.g. https://arxiv.org/pdf/1811.11184.pdf
"""

SUPPORTED_GATES = {"rx", "ry", "rz", "rzx", "rzz", "ryy", "rxx", "p", "u", "controlledgate"}
SUPPORTED_GATES = {"rx", "ry", "rz", "rzx", "rzz", "ryy", "rxx", "p", "u", "controlledgate",
"cx", "cy", "cz", "ccx", "swap", "iswap", "t", "s", "sdg", "x", "y", "z"}

# pylint: disable=signature-differs
def convert(
Expand Down Expand Up @@ -600,8 +601,12 @@ def _gradient_states(
qr_superpos = QuantumRegister(1)
state_qc = QuantumCircuit(*state_op.primitive.qregs, qr_superpos)
state_qc.h(qr_superpos)
state_qc.compose(state_op.primitive, inplace=True)
state_qc = CircuitGradient._unroll_to_supported_operations(state_qc, self.SUPPORTED_GATES)
# unroll separately from the H gate since we need the H gate to be the first
# operation in the data attributes of the circuit
unrolled = CircuitGradient._unroll_to_supported_operations(
state_op.primitive, self.SUPPORTED_GATES)
state_qc.compose(unrolled, inplace=True)

# Define the working qubit to realize the linear combination of unitaries
if not isinstance(target_params, (list, np.ndarray)):
target_params = [target_params]
Expand Down
7 changes: 5 additions & 2 deletions qiskit/opflow/gradients/circuit_qfis/lin_comb_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ def convert(
qr_work = QuantumRegister(1, "work_qubit")
state_qc = QuantumCircuit(*operator.primitive.qregs, qr_work)
state_qc.h(qr_work)
state_qc.compose(operator.primitive, inplace=True)
state_qc = LinComb._unroll_to_supported_operations(state_qc, LinComb.SUPPORTED_GATES)
# unroll separately from the H gate since we need the H gate to be the first
# operation in the data attributes of the circuit
unrolled = LinComb._unroll_to_supported_operations(
operator.primitive, LinComb.SUPPORTED_GATES)
state_qc.compose(unrolled, inplace=True)

# Get the circuits needed to compute〈∂iψ|∂jψ〉
for i, param_i in enumerate(params): # loop over parameters
Expand Down

0 comments on commit c0c74f6

Please sign in to comment.