Python Forum
Triplet Combinations of All Values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Triplet Combinations of All Values
#1
Hello,
I am using a specific library

And I am running this line:
cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)))
And I also have to write this code line with all possible triplet combinations of a and b
Like
        cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(a)))
        cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)))
        cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)))
        cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(b)))
        cirq.kron(cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(a)))
        cirq.kron(cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)))
        cirq.kron(cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)))
        cirq.kron(cirq.unitary(cirq.rz(a)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(a)))
        cirq.kron(cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(b)),cirq.unitary(cirq.rz(b)))
[/python]

How can I do that in the simplest way?
Reply
#2
Probably you could use itertools.product to generate them.

>>> from itertools import product
>>> list(product("ab", repeat=3))
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'b', 'a'), ('b', 'b', 'b')]
So something similar to:
for triplet in product((a, b), repeat=3):
    cirq.kron(cirq.unitary(cirq.rz(triplet[0])),cirq.unitary(cirq.rz(triplet[1])),cirq.unitary(cirq.rz(triplet[2])))
Reply
#3
(Nov-04-2020, 11:38 PM)bowlofred Wrote: Probably you could use itertools.product to generate them.

>>> from itertools import product
>>> list(product("ab", repeat=3))
[('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a'), ('a', 'b', 'b'), ('b', 'a', 'a'), ('b', 'a', 'b'), ('b', 'b', 'a'), ('b', 'b', 'b')]
So something similar to:
for triplet in product((a, b), repeat=3):
    cirq.kron(cirq.unitary(cirq.rz(triplet[0])),cirq.unitary(cirq.rz(triplet[1])),cirq.unitary(cirq.rz(triplet[2])))
Thank you very much :))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 836 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  How can I find all combinations with a regular expression? AlekseyPython 0 1,636 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  All possible combinations CODEP 2 1,826 Dec-01-2020, 06:10 PM
Last Post: deanhystad
  Converting string to hex triplet menator01 4 4,217 Aug-03-2020, 01:00 PM
Last Post: deanhystad
  All possible combinations of multiplications Shreya10o 0 1,632 May-23-2020, 07:45 AM
Last Post: Shreya10o
  Do something with all possible combinations of a list 3Pinter 7 4,013 Sep-11-2019, 08:19 AM
Last Post: perfringo
  list of string combinations Skaperen 8 3,241 May-22-2019, 01:18 PM
Last Post: Skaperen
  Python to iterate a number of possible combinations teflon 4 3,884 Apr-24-2019, 03:00 AM
Last Post: scidam
  Distances between all combinations in datatset amyd 6 3,802 Dec-13-2018, 01:23 PM
Last Post: amyd
  Combinations of list of lists dannyH 2 3,309 May-14-2018, 09:54 PM
Last Post: dannyH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020