View source on GitHub |
Represents control values as AND (product) clauses, each of which applies to all N qubits.
cirq.SumOfProducts(
data: Collection[Sequence[int]], *, name: Optional[str] = None
)
SumOfProducts
representation describes the control values as a union
of n-bit tuples, where each n-bit tuple represents an allowed assignment
of bits for which the control should be activated. This expanded
representation allows us to create control values combinations which
cannot be factored as a ProductOfSums
representation.
For example:
1) (|00><00| + |11><11|) X + (|01><01| + |10><10|) I
represents an
operator which flips the third qubit if the first two qubits
are 00
or 11
, and does nothing otherwise.
This can be constructed as
```
>>> xor_control_values = cirq.SumOfProducts(((0, 0), (1, 1)))
>>> q0, q1, q2 = cirq.LineQubit.range(3)
>>> xor_cop = cirq.X(q2).controlled_by(q0, q1, control_values=xor_control_values)
```
2) (|00><00| + |01><01| + |10><10|) X + (|11><11|) I
represents an
operators which flips the third qubit if the nand
of first two
qubits is 1
(i.e. first two qubits are either 00
, 01
or 10
),
and does nothing otherwise. This can be constructed as:
<pre class="devsite-click-to-copy prettyprint lang-py">
<code class="devsite-terminal" data-terminal-prefix=">>>">nand_control_values = cirq.SumOfProducts(((0, 0), (0, 1), (1, 0)))</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">q0, q1, q2 = cirq.LineQubit.range(3)</code>
<code class="devsite-terminal" data-terminal-prefix=">>>">nand_cop = cirq.X(q2).controlled_by(q0, q1, control_values=nand_control_values)</code>
<code class="no-select nocode"> </code>
</pre>
Attributes | |
---|---|
is_trivial
|
Methods
expand
expand() -> 'SumOfProducts'
Returns an expanded cirq.SumOfProduct
representation of this control values.
validate
validate(
qid_shapes: Sequence[int]
) -> None
Validates that all control values for ith qubit are in range [0, qid_shaped[i])
__and__
__and__(
other: 'AbstractControlValues'
) -> 'AbstractControlValues'
Returns a cartesian product of all control values predicates in self
x other
.
The and
of two control values cv1
and cv2
represents a control value object
acting on the union of qubits represented by cv1
and cv2
. For example:
cv1 = cirq.ProductOfSums([(0, 1), 2])
cv2 = cirq.SumOfProducts([[0, 0], [1, 1]])
assert cirq.num_qubits(cv1 & cv2) == cirq.num_qubits(cv1) + cirq.num_qubits(cv2)
Args | |
---|---|
other
|
An instance of AbstractControlValues .
|
Returns | |
---|---|
An instance of AbstractControlValues that represents the cartesian product of
control values represented by self and other .
|
__eq__
__eq__(
other: _SupportsValueEquality
) -> bool
__iter__
__iter__() -> Iterator[Tuple[int, ...]]
Returns the combinations tracked by the object.
__ne__
__ne__(
other: _SupportsValueEquality
) -> bool
__or__
__or__(
other: 'AbstractControlValues'
) -> 'AbstractControlValues'
Returns a union of all control values predicates in self
+ other
.
Both self
and other
must represent control values for the same set of qubits and
hence their or
would also be a control value object acting on the same set of qubits.
For example:
cv1 = cirq.ProductOfSums([(0, 1), 2])
cv2 = cirq.SumOfProducts([[0, 0], [1, 1]])
assert cirq.num_qubits(cv1 | cv2) == cirq.num_qubits(cv1) == cirq.num_qubits(cv2)
Args | |
---|---|
other
|
An instance of AbstractControlValues .
|
Returns | |
---|---|
An instance of AbstractControlValues that represents the union of control values
represented by self and other .
|
Raises | |
---|---|
ValueError
|
If cirq.num_qubits(self) != cirq.num_qubits(other) .
|