qml.wires.Wires¶
- class Wires(wires, _override=False)[source]¶
Bases:
collections.abc.Sequence
A bookkeeping class for wires, which are ordered collections of unique objects.
If the input
wires
can be iterated over, it is interpreted as a sequence of wire labels that have to be unique and hashable. Else it is interpreted as a single wire label that has to be hashable. The only exception are strings which are interpreted as wire labels.The hash function of a wire label is considered the source of truth when deciding whether two wire labels are the same or not.
Indexing an instance of this class will return a wire label.
Warning
In order to support wire labels of any hashable type, integers and 0-d arrays are considered different. For example, running
qml.RX(1.1, qml.numpy.array(0))
on a device initialized withwires=[0]
will fail becauseqml.numpy.array(0)
does not exist in the device’s wire map.- Parameters
wires (Any) – the wire label(s)
Attributes
Get a tuple of the labels of this Wires object.
- labels¶
Get a tuple of the labels of this Wires object.
Methods
all_wires
(list_of_wires[, sort])Return the wires that appear in any of the Wires objects in the list.
contains_wires
(wires)Method to determine if Wires object contains wires in another Wires object.
count
(value)difference
(other)Return the difference of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.index
(wire)Overwrites a Sequence's
index()
function which returns the index ofwire
.indices
(wires)Return the indices of the wires in this Wires object.
intersection
(other)Return the intersection of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.map
(wire_map)Returns a new Wires object with different labels, using the rule defined in mapping.
select_random
(n_samples[, seed])Returns a randomly sampled subset of Wires of length 'n_samples'.
shared_wires
(list_of_wires)Return only the wires that appear in each Wires object in the list.
subset
(indices[, periodic_boundary])Returns a new Wires object which is a subset of this Wires object.
symmetric_difference
(other)Return the symmetric difference of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.toarray
()Returns a numpy array representation of the Wires object.
tolist
()Returns a list representation of the Wires object.
toset
()Returns a set representation of the Wires object.
union
(other)Return the union of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.unique_wires
(list_of_wires)Return the wires that are unique to any Wire object in the list.
- static all_wires(list_of_wires, sort=False)[source]¶
Return the wires that appear in any of the Wires objects in the list.
This is similar to a set combine method, but keeps the order of wires as they appear in the list.
- Parameters
list_of_wires (list[Wires]) – list of Wires objects
sort (bool) – Toggle for sorting the combined wire labels. The sorting is based on value if all keys are int, else labels’ str representations are used.
- Returns
combined wires
- Return type
Example
>>> wires1 = Wires([4, 0, 1]) >>> wires2 = Wires([3, 0, 4]) >>> wires3 = Wires([5, 3]) >>> list_of_wires = [wires1, wires2, wires3] >>> Wires.all_wires(list_of_wires) Wires([4, 0, 1, 3, 5])
- contains_wires(wires)[source]¶
Method to determine if Wires object contains wires in another Wires object.
- count(value) integer -- return number of occurrences of value ¶
- difference(other)[source]¶
Return the difference of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.- Parameters
other (Any) –
Wires
object or any iterable that can be interpreted like aWires
object to perform the difference with- Returns
A new
Wires
object representing the difference of the twoWires
objects.- Return type
Example
>>> from pennylane.wires import Wires >>> wires1 = Wires([1, 2, 3]) >>> wires2 = Wires([2, 3, 4]) >>> wires1.difference(wires2) Wires([1])
Alternatively, use the
-
operator:>>> wires1 - wires2 Wires([1])
- index(wire)[source]¶
Overwrites a Sequence’s
index()
function which returns the index ofwire
.- Parameters
wire (Any) – Object whose index is to be found. If this is a Wires object of length 1, look for the object representing the wire.
- Returns
index of the input
- Return type
int
- indices(wires)[source]¶
Return the indices of the wires in this Wires object.
- Parameters
wires (Iterable[Number, str], Number, str, Wires) – Wire(s) whose indices are to be found
- Returns
index list
- Return type
list
Example
>>> wires1 = Wires([4, 0, 1]) >>> wires2 = Wires([1, 4]) >>> wires1.indices(wires2) [2, 0] >>> wires1.indices([1, 4]) [2, 0]
- intersection(other)[source]¶
Return the intersection of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.- Parameters
other (Any) –
Wires
or any iterable that can be interpreted like aWires
object to perform the intersection with- Returns
A new
Wires
object representing the intersection of the twoWires
objects.- Return type
Example
>>> from pennylane.wires import Wires >>> wires1 = Wires([1, 2, 3]) >>> wires2 = Wires([2, 3, 4]) >>> wires1.intersection(wires2) Wires([2, 3])
Alternatively, use the
&
operator:>>> wires1 & wires2 Wires([2, 3])
- map(wire_map)[source]¶
Returns a new Wires object with different labels, using the rule defined in mapping.
- Parameters
wire_map (dict) – Dictionary containing all wire labels used in this object as keys, and unique new labels as their values
Example
>>> wires = Wires(['a', 'b', 'c']) >>> wire_map = {'a': 4, 'b':2, 'c': 3} >>> wires.map(wire_map) Wires([4, 2, 3])
- select_random(n_samples, seed=None)[source]¶
Returns a randomly sampled subset of Wires of length ‘n_samples’.
- Parameters
n_samples (int) – number of subsampled wires
seed (int) – optional random seed used for selecting the wires
- Returns
random subset of wires
- Return type
Return only the wires that appear in each Wires object in the list.
This is similar to a set intersection method, but keeps the order of wires as they appear in the list.
- Parameters
list_of_wires (list[Wires]) – list of Wires objects
- Returns
shared wires
- Return type
Example
>>> wires1 = Wires([4, 0, 1]) >>> wires2 = Wires([3, 0, 4]) >>> wires3 = Wires([4, 0]) >>> Wires.shared_wires([wires1, wires2, wires3]) Wires([4, 0]) >>> Wires.shared_wires([wires2, wires1, wires3]) Wires([0, 4])
- subset(indices, periodic_boundary=False)[source]¶
Returns a new Wires object which is a subset of this Wires object. The wires of the new object are the wires at positions specified by ‘indices’. Also accepts a single index as input.
- Parameters
indices (List[int] or int) – indices or index of the wires we want to select
periodic_boundary (bool) – controls periodic boundary conditions in the indexing
- Returns
subset of wires
- Return type
Example
>>> wires = Wires([4, 0, 1, 5, 6]) >>> wires.subset([2, 3, 0]) Wires([1, 5, 4]) >>> wires.subset(1) Wires([0])
If
periodic_boundary
is True, the modulo of the number of wires of an index is used instead of an index, so thatwires.subset(i) == wires.subset(i % n_wires)
wheren_wires
is the number of wires of this object.>>> wires = Wires([4, 0, 1, 5, 6]) >>> wires.subset([5, 1, 7], periodic_boundary=True) Wires([4, 0, 1])
- symmetric_difference(other)[source]¶
Return the symmetric difference of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.- Parameters
other (Any) –
Wires
or any iterable that can be interpreted like aWires
object to perform the symmetric difference with- Returns
A new
Wires
object representing the symmetric difference of the twoWires
objects.- Return type
Example
>>> from pennylane.wires import Wires >>> wires1 = Wires([1, 2, 3]) >>> wires2 = Wires([3, 4, 5]) >>> wires1.symmetric_difference(wires2) Wires([1, 2, 4, 5])
Alternatively, use the
^
operator:>>> wires1 ^ wires2 Wires([1, 2, 4, 5])
- toarray()[source]¶
Returns a numpy array representation of the Wires object.
- Returns
array representing Wires object
- Return type
ndarray
- tolist()[source]¶
Returns a list representation of the Wires object.
- Returns
list of wire labels
- Return type
List
- toset()[source]¶
Returns a set representation of the Wires object.
- Returns
set of wire labels
- Return type
Set
- union(other)[source]¶
Return the union of the current
Wires
object and either anotherWires
object or an iterable that can be interpreted like aWires
object, e.g., alist
.- Parameters
other (Any) –
Wires
or any iterable that can be interpreted like aWires
object to perform the union with- Returns
A new
Wires
object representing the union of the twoWires
objects.- Return type
Example
>>> from pennylane.wires import Wires >>> wires1 = Wires([1, 2, 3]) >>> wires2 = Wires([3, 4, 5]) >>> wires1.union(wires2) Wires([1, 2, 3, 4, 5])
Alternatively, use the
|
operator:>>> wires1 | wires2 Wires([1, 2, 3, 4, 5])
- static unique_wires(list_of_wires)[source]¶
Return the wires that are unique to any Wire object in the list.
- Parameters
list_of_wires (list[Wires]) – list of Wires objects
- Returns
unique wires
- Return type
Example
>>> wires1 = Wires([4, 0, 1]) >>> wires2 = Wires([0, 2, 3]) >>> wires3 = Wires([5, 3]) >>> Wires.unique_wires([wires1, wires2, wires3]) Wires([4, 1, 2, 5])