Beispiel #1
0
 def _get_output_shape_from_op(self, op: OperatorBase) -> Tuple[int, ...]:
     """Determines the output shape of a given operator."""
     # TODO: should eventually be moved to opflow
     if isinstance(op, ListOp):
         shapes = []
         for op_ in op.oplist:
             shape_ = self._get_output_shape_from_op(op_)
             shapes += [shape_]
         if not np.all([shape == shapes[0] for shape in shapes]):
             raise QiskitMachineLearningError(
                 'Only supports ListOps with children that return the same shape.')
         if shapes[0] == (1,):
             out = op.combo_fn(np.zeros((len(op.oplist))))
         else:
             out = op.combo_fn(np.zeros((len(op.oplist), *shapes[0])))
         return out.shape
     else:
         return (1,)
 def _compute_output_shape(self, op: OperatorBase) -> Tuple[int, ...]:
     """Determines the output shape of a given operator."""
     # TODO: the whole method should eventually be moved to opflow and rewritten in a better way.
     # if the operator is a composed one, then we only need to look at the first element of it.
     if isinstance(op, ComposedOp):
         return self._compute_output_shape(op.oplist[0].primitive)
     # this "if" statement is on purpose, to prevent sub-classes.
     # pylint:disable=unidiomatic-typecheck
     if type(op) == ListOp:
         shapes = [self._compute_output_shape(op_) for op_ in op.oplist]
         if not np.all([shape == shapes[0] for shape in shapes]):
             raise QiskitMachineLearningError(
                 "Only supports ListOps with children that return the same shape."
             )
         if shapes[0] == (1, ):
             out = op.combo_fn(np.zeros((len(op.oplist))))
         else:
             out = op.combo_fn(np.zeros((len(op.oplist), *shapes[0])))
         return out.shape
     else:
         return (1, )
 def _get_output_shape_from_op(self, op: OperatorBase) -> Tuple[int, ...]:
     """Determines the output shape of a given operator."""
     # TODO: should eventually be moved to opflow
     # this "if" statement is on purpose, to prevent sub-classes.
     # pylint:disable=unidiomatic-typecheck
     if type(op) == ListOp:
         shapes = []
         for op_ in op.oplist:
             shape_ = self._get_output_shape_from_op(op_)
             shapes += [shape_]
         if not np.all([shape == shapes[0] for shape in shapes]):
             raise QiskitMachineLearningError(
                 'Only supports ListOps with children that return the same shape.'
             )
         if shapes[0] == (1, ):
             out = op.combo_fn(np.zeros((len(op.oplist))))
         else:
             out = op.combo_fn(np.zeros((len(op.oplist), *shapes[0])))
         return out.shape
     else:
         return (1, )