Esempio n. 1
0
def test_gate_on_operation_besides_gate_operation():
    a, b = cirq.LineQubit.range(2)

    assert cirq.op_gate_of_type(
        -1j * cirq.X(a) * cirq.Y(b),
        cirq.DensePauliString) == -1j * cirq.DensePauliString('XY')

    assert cirq.op_gate_isinstance(-1j * cirq.X(a) * cirq.Y(b),
                                   cirq.DensePauliString)

    assert not cirq.op_gate_isinstance(-1j * cirq.X(a) * cirq.Y(b),
                                       cirq.XPowGate)
Esempio n. 2
0
def test_op_gate_isinstance():
    a = cirq.NamedQubit('a')
    op = cirq.X(a)
    assert cirq.op_gate_isinstance(op, cirq.XPowGate)
    assert not cirq.op_gate_isinstance(op, cirq.YPowGate)

    class NonGateOperation(cirq.Operation):
        def qubits(self):
            pass

        def with_qubits(self, *new_qubits):
            pass

    assert not cirq.op_gate_isinstance(NonGateOperation(), cirq.XPowGate)
    assert not cirq.op_gate_isinstance(NonGateOperation(), NonGateOperation)
Esempio n. 3
0
 def simulate_op(op, temp_state):
     indices = [qubit_map[q] for q in op.qubits]
     if cirq.op_gate_isinstance(op, cirq.ResetChannel):
         self._simulate_reset(op, cirq.ResetChannel)
     elif cirq.is_measurement(op):
         if perform_measurements:
             self._simulate_measurement(
                 op, temp_state, indices, measurements)
     elif cirq.has_mixture(op):
         self._simulate_mixture(op, temp_state, indices)
     else:
         if cirq.num_qubits(op) <= 3:
             self._simulate_unitary(op, temp_state, indices)
         else:
             decomp_ops = cirq.decompose_once(op, default=None)
             if decomp_ops is None:
                 self._simulate_unitary(op, temp_state, indices)
             else:
                 for sub_op in cirq.flatten_op_tree(decomp_ops):
                     simulate_op(sub_op, temp_state)
Esempio n. 4
0
 def simulate_op(op, temp_state):
     indices = [qubit_map[q] for q in op.qubits]
     if cirq.op_gate_isinstance(op, cirq.ResetChannel):
         self._simulate_reset(op, cirq.ResetChannel)
     elif cirq.is_measurement(op):
         if perform_measurements:
             self._simulate_measurement(
                 op, temp_state, indices, measurements)
     elif cirq.has_mixture(op):
         self._simulate_mixture(op, temp_state, indices)
     else:
         decomp_ops = cirq.decompose_once(op, default=None)
         if decomp_ops is None:
             self._simulate_unitary(op, temp_state, indices)
         else:
             try:
                 temp2_state = temp_state.copy()
                 for sub_op in cirq.flatten_op_tree(decomp_ops):
                     simulate_op(sub_op, temp2_state)
                 temp_state[...] = temp2_state
             except ValueError:
                 # Non-classical unitary in the decomposition
                 self._simulate_unitary(op, temp_state, indices)
Esempio n. 5
0
 def keep(potential_op):
     return (cirq.has_unitary(potential_op) or
             cirq.has_mixture(potential_op) or
             cirq.is_measurement(potential_op) or
             cirq.op_gate_isinstance(potential_op, cirq.ResetChannel))