def test_transform_leaves(): gs = [Gate() for _ in range(10)] operations = [ Operation(gs[i], [NamedQubit(str(i))]) for i in range(10) ] expected = [ Operation(gs[i], [NamedQubit(str(i) + 'a')]) for i in range(10) ] def move_left(op): return Operation(op.gate, [NamedQubit(q.name + 'a') for q in op.qubits]) def move_tree_left_freeze(root): return op_tree.freeze_op_tree( op_tree.transform_op_tree(root, move_left)) # Empty tree. assert move_tree_left_freeze([[[]]]) == (((),),) # Just an item. assert move_tree_left_freeze(operations[0]) == expected[0] # Flat list. assert move_tree_left_freeze(operations) == tuple(expected) # Tree. assert ( move_tree_left_freeze( (operations[0], operations[1:5], operations[5:])) == (expected[0], tuple(expected[1:5]), tuple(expected[5:])))
def test_map(): b = NamedQubit('b!') q = QubitOrder.explicit([NamedQubit('b')]).map( internalize=lambda e: NamedQubit(e.name[:-1]), externalize=lambda e: NamedQubit(e.name + '!')) assert q.order_for([]) == (b,) assert q.order_for([b]) == (b,)
def test_sorted_by(): a = NamedQubit('2') b = NamedQubit('10') c = NamedQubit('-5') q = QubitOrder.sorted_by(lambda e: -int(str(e))) assert q.order_for([]) == () assert q.order_for([a]) == (a,) assert q.order_for([a, b]) == (b, a) assert q.order_for([a, b, c]) == (b, a, c)
def test_explicit_with_fallback(): a2 = NamedQubit('a2') a10 = NamedQubit('a10') b = NamedQubit('b') q = QubitOrder.explicit([b], fallback=QubitOrder.DEFAULT) assert q.order_for([]) == (b,) assert q.order_for([b]) == (b,) assert q.order_for([b, a2]) == (b, a2) assert q.order_for([a2]) == (b, a2) assert q.order_for([a10, a2]) == (b, a2, a10)
def test_explicit(): a2 = NamedQubit('a2') a10 = NamedQubit('a10') b = NamedQubit('b') with pytest.raises(ValueError): _ = QubitOrder.explicit([b, b]) q = QubitOrder.explicit([a10, a2, b]) assert q.order_for([b]) == (a10, a2, b) assert q.order_for([a2]) == (a10, a2, b) assert q.order_for([]) == (a10, a2, b) with pytest.raises(ValueError): _ = q.order_for([NamedQubit('c')])
def test_qubit_order_or_list(): b = NamedQubit('b') implied_by_list = QubitOrder.as_qubit_order([b]) assert implied_by_list.order_for([]) == (b,) implied_by_generator = QubitOrder.as_qubit_order( NamedQubit(e.name + '!') for e in [b]) assert implied_by_generator.order_for([]) == (NamedQubit('b!'),) assert implied_by_generator.order_for([]) == (NamedQubit('b!'),) ordered = QubitOrder.sorted_by(repr) passed_through = QubitOrder.as_qubit_order(ordered) assert ordered is passed_through
def _convert_qubit(qb: Qubit, indexed_qubits: List[Qid]) -> cirq.Qid: if qb.reg.name == "q": if indexed_qubits: return indexed_qubits[qb.index] return LineQubit(qb.index) else: return NamedQubit(qb.__repr__())
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,)])
def test_flatten_op_tree(): operations = [ Operation(Gate(), [NamedQubit(str(i))]) for i in range(10) ] # Empty tree. assert list(op_tree.flatten_op_tree([[[]]])) == [] # Just an item. assert list(op_tree.flatten_op_tree(operations[0])) == operations[:1] # Flat list. assert list(op_tree.flatten_op_tree(operations)) == operations # Tree. assert list(op_tree.flatten_op_tree((operations[0], operations[1:5], operations[5:]))) == operations # Bad trees. with pytest.raises(TypeError): _ = list(op_tree.flatten_op_tree(None)) with pytest.raises(TypeError): _ = list(op_tree.flatten_op_tree(5)) with pytest.raises(TypeError): _ = list(op_tree.flatten_op_tree([operations[0], (4,)]))
def test_transform_internal_nodes(): operations = [ Operation(Gate(), [NamedQubit(str(2 * i))]) for i in range(10) ] def skip_first(op): first = True for item in op: if not first: yield item first = False def skip_tree_freeze(root): return op_tree.freeze_op_tree( op_tree.transform_op_tree(root, iter_transformation=skip_first)) # Empty tree. assert skip_tree_freeze([[[]]]) == () assert skip_tree_freeze([[[]], [[], []]]) == (((),),) # Just an item. assert skip_tree_freeze(operations[0]) == operations[0] # Flat list. assert skip_tree_freeze(operations) == tuple(operations[1:]) # Tree. assert ( skip_tree_freeze((operations[1:5], operations[0], operations[5:])) == (operations[0], tuple(operations[6:])))
def test_default_noise(): p_qubits = cirq.NamedQubit.range(2, prefix='q') p_device = PasqalDevice(qubits=p_qubits) noise_model = PasqalNoiseModel(p_device) circuit = cirq.Circuit() Gate_l = cirq.ops.CZPowGate(exponent=2) circuit.append(Gate_l.on(p_qubits[0], p_qubits[1])) p_circuit = cirq.Circuit(circuit, device=p_device) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [[ cirq.ops.CZPowGate(exponent=2).on(NamedQubit('q0'), NamedQubit('q1')), cirq.depolarize(p=0.05).on(NamedQubit('q0')), cirq.depolarize(p=0.05).on(NamedQubit('q1')), ]]
def test_noisy_moments(): p_qubits = cirq.NamedQubit.range(2, prefix='q') p_device = PasqalDevice(qubits=p_qubits) noise_model = PasqalNoiseModel(p_device) circuit = cirq.Circuit() circuit.append(cirq.ops.CZ(p_qubits[0], p_qubits[1])) circuit.append(cirq.ops.Z(p_qubits[1])) p_circuit = cirq.Circuit(circuit) n_mts = [] for moment in p_circuit._moments: n_mts.append(noise_model.noisy_moment(moment, p_qubits)) assert n_mts == [ [ cirq.ops.CZ.on(NamedQubit('q0'), NamedQubit('q1')), cirq.depolarize(p=0.03).on(NamedQubit('q0')), cirq.depolarize(p=0.03).on(NamedQubit('q1')), ], [cirq.ops.Z.on(NamedQubit('q1')), cirq.depolarize(p=0.01).on(NamedQubit('q1'))], ]
def test_default(): a2 = NamedQubit('a2') a10 = NamedQubit('a10') b = NamedQubit('b') assert QubitOrder.DEFAULT.order_for([]) == () assert QubitOrder.DEFAULT.order_for([a10, a2, b]) == (a2, a10, b)
def move_left(op): return Operation(op.gate, [NamedQubit(q.name + 'a') for q in op.qubits])