Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Add a parameters method to the Operators (#1194)
Browse files Browse the repository at this point in the history
Co-authored-by: Manoel Marques <Manoel.Marques@ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 18, 2020
1 parent 2444e9a commit 408711a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
9 changes: 9 additions & 0 deletions qiskit/aqua/operators/list_ops/list_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ def __repr__(self) -> str:
self.coeff,
self.abelian)

@property
def parameters(self):
params = set()
for op in self.oplist:
params.update(op.parameters)
if isinstance(self.coeff, ParameterExpression):
params.update(self.coeff.parameters)
return params

def assign_parameters(self, param_dict: dict) -> OperatorBase:
param_value = self.coeff
if isinstance(self.coeff, ParameterExpression):
Expand Down
7 changes: 7 additions & 0 deletions qiskit/aqua/operators/operator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ def tensorpower(self, other: int) -> Union['OperatorBase', int]:
"""
raise NotImplementedError

@property
@abstractmethod
def parameters(self):
r""" Return a set of Parameter objects contained in the Operator.
"""
raise NotImplementedError

# Utility functions for parameter binding

@abstractmethod
Expand Down
9 changes: 9 additions & 0 deletions qiskit/aqua/operators/primitive_ops/primitive_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ def eval(self,
OperatorBase] = None) -> Union[OperatorBase, float, complex]:
raise NotImplementedError

@property
def parameters(self):
params = set()
if isinstance(self.primitive, (OperatorBase, QuantumCircuit)):
params.update(self.primitive.parameters)
if isinstance(self.coeff, ParameterExpression):
params.update(self.coeff.parameters)
return params

def assign_parameters(self, param_dict: dict) -> OperatorBase:
param_value = self.coeff
if isinstance(self.coeff, ParameterExpression):
Expand Down
9 changes: 9 additions & 0 deletions qiskit/aqua/operators/state_fns/state_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ def eval(self,
OperatorBase] = None) -> Union[OperatorBase, float, complex]:
raise NotImplementedError

@property
def parameters(self):
params = set()
if isinstance(self.primitive, (OperatorBase, QuantumCircuit)):
params.update(self.primitive.parameters)
if isinstance(self.coeff, ParameterExpression):
params.update(self.coeff.parameters)
return params

def assign_parameters(self, param_dict: dict) -> OperatorBase:
param_value = self.coeff
if isinstance(self.coeff, ParameterExpression):
Expand Down
57 changes: 55 additions & 2 deletions test/aqua/operators/test_op_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import numpy as np
from ddt import ddt, data

from qiskit.circuit import QuantumCircuit, QuantumRegister, Instruction, Parameter
from qiskit.circuit import QuantumCircuit, QuantumRegister, Instruction, Parameter, ParameterVector
from qiskit.extensions.exceptions import ExtensionError
from qiskit.quantum_info.operators import Operator, Pauli
from qiskit.circuit.library import CZGate, ZGate

from qiskit.aqua.operators import (
X, Y, Z, I, CX, T, H, PrimitiveOp, SummedOp, PauliOp, Minus, CircuitOp, MatrixOp, ListOp,
ComposedOp
ComposedOp, StateFn
)


Expand Down Expand Up @@ -447,6 +447,59 @@ def test_op_indent(self, op):
).split("\n{}".format(op.INDENTATION))
self.assertListEqual(indented_str_content, initial_str.split("\n"))

def test_op_parameters(self):
"""Test that Parameters are stored correctly"""
phi = Parameter('φ')
theta = ParameterVector(name='θ',
length=2)

qc = QuantumCircuit(2)
qc.rz(phi, 0)
qc.rz(phi, 1)
for i in range(2):
qc.rx(theta[i], i)
qc.h(0)
qc.x(1)

l = Parameter('λ')
op = PrimitiveOp(qc,
coeff=l)

params = set([phi, l, *theta.params])

self.assertEqual(params, op.parameters)
self.assertEqual(params, StateFn(op).parameters)
self.assertEqual(params, StateFn(qc, coeff=l).parameters)

def test_list_op_parameters(self):
"""Test that Parameters are stored correctly in a List Operator"""
lam = Parameter('λ')
phi = Parameter('φ')
omega = Parameter('ω')

mat_op = PrimitiveOp([[0, 1],
[1, 0]],
coeff=omega)

qc = QuantumCircuit(1)
qc.rx(phi, 0)
qc_op = PrimitiveOp(qc)

op1 = SummedOp([mat_op, qc_op])

params = [phi, omega]
self.assertEqual(op1.parameters, set(params))

# check list nesting case
op2 = PrimitiveOp([[1, 0],
[0, -1]],
coeff=lam)

list_op = ListOp([op1, op2])

params.append(lam)
self.assertEqual(list_op.parameters, set(params))


class TestListOpComboFn(QiskitAquaTestCase):
"""Test combo fn is propagated."""
Expand Down

0 comments on commit 408711a

Please sign in to comment.