def test_coverage(): q = cirq.LineQubit.range(3) g = cirq.ThreeQubitGate() class FakeOperation(ops.Operation): def __init__(self, gate, qubits): self._gate = gate self._qubits = qubits @property def qubits(self): return self._qubits def with_qubits(self, *new_qubits): return FakeOperation(self._gate, new_qubits) op = FakeOperation(g, q).with_qubits(*q) c = cirq.Circuit(cirq.X.on(q[0])) cirq.neutral_atoms.ConvertToNeutralAtomGates().optimize_circuit(c) assert c == cirq.Circuit(cirq.X.on(q[0])) assert (cirq.neutral_atoms.ConvertToNeutralAtomGates().convert( cirq.X.on(q[0])) == [cirq.X.on(q[0])]) with pytest.raises(TypeError, match="Don't know how to work with"): cirq.neutral_atoms.ConvertToNeutralAtomGates().convert(op) assert not cirq.neutral_atoms.is_native_neutral_atom_op(op)
def test_invalid_identity_operation(): three_qubit_gate = cirq.ThreeQubitGate() with pytest.raises(ValueError, match="empty set of qubits"): cirq.IdentityOperation([]) with pytest.raises(ValueError, match="Gave non-Qid objects to IdentityOperation"): cirq.IdentityOperation([three_qubit_gate])
def test_with_qubits_and_transform_qubits(): g = cirq.ThreeQubitGate() op = cirq.GateOperation(g, cirq.LineQubit.range(3)) assert op.with_qubits(*cirq.LineQubit.range(3, 0, -1)) \ == cirq.GateOperation(g, cirq.LineQubit.range(3, 0, -1)) assert op.transform_qubits(lambda e: cirq.LineQubit(-e.x) ) == cirq.GateOperation(g, [cirq.LineQubit(0), cirq.LineQubit(-1), cirq.LineQubit(-2)])
def test_invalid_parallel_gate_operation(): three_qubit_gate = cirq.ThreeQubitGate() single_qubit_gate = cirq.SingleQubitGate() repeated_qubits = [cirq.GridQubit(0, 0), cirq.GridQubit(0, 0)] with pytest.raises(ValueError) as wrong_gate: cirq.ParallelGateOperation(three_qubit_gate, cirq.NamedQubit("a")) assert str(wrong_gate.value) == "gate must be a single qubit gate" with pytest.raises(ValueError) as bad_qubits: cirq.ParallelGateOperation(single_qubit_gate, repeated_qubits) assert str(bad_qubits.value) == "repeated qubits are not allowed"
def test_with_qubits_and_transform_qubits(): g = cirq.ThreeQubitGate() op = cirq.GateOperation(g, cirq.LineQubit.range(3)) assert op.with_qubits(*cirq.LineQubit.range(3, 0, -1)) \ == cirq.GateOperation(g, cirq.LineQubit.range(3, 0, -1)) assert op.transform_qubits(lambda e: cirq.LineQubit(-e.x) ) == cirq.GateOperation(g, [cirq.LineQubit(0), cirq.LineQubit(-1), cirq.LineQubit(-2)]) # The gate's constraints should be applied when changing the qubits. with pytest.raises(ValueError): _ = cirq.H(cirq.LineQubit(0)).with_qubits(cirq.LineQubit(0), cirq.LineQubit(1))
def test_invalid_gate_operation(): three_qubit_gate = cirq.ThreeQubitGate() single_qubit = [cirq.GridQubit(0, 0)] with pytest.raises(ValueError, match="number of qubits"): cirq.GateOperation(three_qubit_gate, single_qubit)
def test_invalid_gate_operation(): three_qubit_gate = cirq.ThreeQubitGate() single_qubit = [cirq.GridQubit(0, 0)] with pytest.raises(ValueError) as bad: cirq.GateOperation(three_qubit_gate, single_qubit) assert 'Three-qubit gate not applied to three qubits:' in str(bad.value)