0

Suppose I have the following command (using Python and Qiskit):

a = reduce(lambda x,y: x.compose(y,c),circli, qcla)

(qcla is an initializer)

Here, compose is an internal qiskit function, x and y are elements of the list circli (iterable). I'm wondering is it possible for me to add another iterable in this reduce function? Here, c itself in (y,c) represents a coordinate, such as [2,3], and I'm hoping to have it updated as well. Can I create another list containing all the possible c's and add it as another iterable? Thanks!

ZR-
  • 809
  • 1
  • 4
  • 12
  • 1
    Reduce isn't some magic panacea. It takes a sequence of objects and repeatedly performs a binary operation by accumulating the results: `{1, 2, 3; +} -> 1 + (2 + 3) -> 1 + 5 -> 6`. Does it make sense for the `compose` function to be able to do that with two iterable sequences? Doesn't look like it to me. What do you mean by "hoping to have it updated as well"? Can you give an example input and output? Depending on exactly what you're trying to do, it may be possible to define a function which does what you want, but it may not be worth the effort. – ddejohn Feb 20 '21 at 07:18
  • @blorgon Thanks so much for the comment! I think my question is if I could call the same function ```compose``` differently while using the ```reduce``` function. The ```compose``` function takes an argument of 'coordinate', so I'm wondering if this function has similar usage like '+', '*' in a simpler case of ```reduce```. – ZR- Feb 20 '21 at 07:50
  • Could you come up with an example of an input and the desired output? – ddejohn Feb 20 '21 at 07:52

1 Answers1

2

It is possible to reduce a list (iterable) of pairs and also to create a pair at the end. For the first one you need the zip function, for the latter one you need to modify the lambda function to return a tuple. For example:

a, x_sum = reduce(lambda x,y: (x[0].compose(y[0], y[1]), x[1][0]+y[1][0]), zip(circli, cs), (qcla, 0))

I use the name cs for the list of c values here.

zip creates pairs of circli and cs items and you can go through the pairs. In this case I get the sum of the x coordinates of c values as the result as well.

If the values in cs is the constant c in your example (cs = [c]*len(circli)) , the a in the result will be the same as in your example.