Apply gate to second qubit in ket?
Closed this issue · 2 comments
Hi, I am relatively new to programming and some of the conventions are not as clear to me.
I understand that
X qs
applies an X gate to the first qubit in the list, also that
X >< qs
applies an X to all the qubits in the list. I am unsure how to apply an X to only the second qubit, or to a specific selection of qubits (e.g. 3,5,8). What would be the best way to do this?
Thank you!
EDIT: I think I might have solved it- I did
X qs.[1..]
is this good practice?
Your solution is perfectly reasonable. Since what you want to do is create a list of qubits from another list, LIQUi|> provides a helper function !!
(called: bangband) that extracts qubits in the way you asked. For example:
!!(qs,1)
would create a new qubit list equivalent to[qs.[1]]
!!(qs,1,3,7)
would create a new qubit list equivalent to[qs.[1];qs.[3];qs.[7]]
There are several variations described in the Operations.BangBang documentation (sorry, the help system won't let me call it !!
directly). Here are all the variants listed there:
- A Ket, which is interpreted as the complete list of Qubits in the Ket.
- A single Qubit, which is interpreted as a single-element list.
- A two-tuple of Qubits, which is interpreted as a two-element list.
- A three-tuple of Qubits, which is interpreted as a three-element list.
- A list of Qubits, which is returned directly.
- A two-tuple of lists of Qubits, which are concatenated into a single list.
- A three-tuple of lists of Qubits, which are concatenated into a single list.
- A list of lists of Qubits, which are concatenated into a single list.
- A two-tuple of a list of Qubits and a single Qubit, which are concatenated into a single list.
- A two-tuple of a single Qubit and a list of Qubits, which are concatenated into a single list.
- A two-tuple of a list of Qubits and an integer, which is interpreted as the single-element list containing the item in the list indexed by the integer.
- A three-tuple of a list of Qubits and two integers, which is interpreted as the two-element list containing the items in the list indexed by the two integers.
- A four-tuple of a list of Qubits and three integers, which is interpreted as the three-element list containing the item in the list indexed by the three integers.
- A two-tuple of a list of Qubits and a list of integers, which is interpreted as the list containing the items in the Qubit list indexed by the elements in the integer list.
Also, if you look in the *.fsx
files in the samples
directory, you'll find all sorts of uses of !!
.
Thanks for the reply! Really helpful