Example #1
0
def test_freeze_op_tree():
    operations = [
        Operation(Gate(), [NamedQubit(str(i))])
        for i in range(10)
    ]

    # Empty tree.
    assert op_tree.freeze_op_tree([[[]]]) == (((),),)

    # Just an item.
    assert op_tree.freeze_op_tree(operations[0]) == operations[0]

    # Flat list.
    assert op_tree.freeze_op_tree(operations) == tuple(operations)

    # Tree.
    assert (
        op_tree.freeze_op_tree(
            (operations[0], (operations[i] for i in range(1, 5)),
             operations[5:])) ==
        (operations[0], tuple(operations[1:5]), tuple(operations[5:])))

    # Bad trees.
    with pytest.raises(TypeError):
        op_tree.freeze_op_tree(None)
    with pytest.raises(TypeError):
        op_tree.freeze_op_tree(5)
    with pytest.raises(TypeError):
        _ = op_tree.freeze_op_tree([operations[0], (4,)])
Example #2
0
 def _decompose_(
     self, qubits: Tuple['cirq.Qid', ...]
 ) -> 'protocols.decompose_protocol.DecomposeResult':
     any_qubit = qubits[0]
     to_z_ops = op_tree.freeze_op_tree(self.observable().on(*qubits).to_z_basis_ops())
     xor_decomp = tuple(pauli_string_phasor.xor_nonlocal_decompose(qubits, any_qubit))
     yield to_z_ops
     yield xor_decomp
     yield measurement_gate.MeasurementGate(1, self.mkey).on(any_qubit)
     yield protocols.inverse(xor_decomp)
     yield protocols.inverse(to_z_ops)
Example #3
0
    def _decompose_(self, qubits: Sequence['cirq.Qid']) -> 'cirq.OP_TREE':
        if len(self.dense_pauli_string) <= 0:
            return
        any_qubit = qubits[0]
        to_z_ops = op_tree.freeze_op_tree(self._to_z_basis_ops(qubits))
        xor_decomp = tuple(xor_nonlocal_decompose(qubits, any_qubit))
        yield to_z_ops
        yield xor_decomp

        if self.exponent_neg:
            yield pauli_gates.Z(any_qubit)**self.exponent_neg
        if self.exponent_pos:
            yield pauli_gates.X(any_qubit)
            yield pauli_gates.Z(any_qubit)**self.exponent_pos
            yield pauli_gates.X(any_qubit)

        yield protocols.inverse(xor_decomp)
        yield protocols.inverse(to_z_ops)
Example #4
0
    def _decompose_(self) -> op_tree.OP_TREE:
        if len(self.pauli_string) <= 0:
            return
        qubits = self.qubits
        any_qubit = qubits[0]
        to_z_ops = op_tree.freeze_op_tree(self.pauli_string.to_z_basis_ops())
        xor_decomp = tuple(xor_nonlocal_decompose(qubits, any_qubit))
        yield to_z_ops
        yield xor_decomp

        if self.exponent_neg:
            yield pauli_gates.Z(any_qubit)**self.exponent_neg
        if self.exponent_pos:
            yield pauli_gates.X(any_qubit)
            yield pauli_gates.Z(any_qubit)**self.exponent_pos
            yield pauli_gates.X(any_qubit)

        yield protocols.inverse(xor_decomp)
        yield protocols.inverse(to_z_ops)
Example #5
0
 def skip_tree_freeze(root):
     return op_tree.freeze_op_tree(
         op_tree.transform_op_tree(root, iter_transformation=skip_first))
Example #6
0
 def move_tree_left_freeze(root):
     return op_tree.freeze_op_tree(
         op_tree.transform_op_tree(root, move_left))