def test_inverse_of_invertible_op_tree(): def rev_freeze(root): return ops.freeze_op_tree(ops.inverse_of_invertible_op_tree(root)) operations = [ ops.Operation(_FlipGate(i), [ops.NamedQubit(str(i))]) for i in range(10) ] expected = [ ops.Operation(_FlipGate(~i), [ops.NamedQubit(str(i))]) for i in range(10) ] # Just an item. assert rev_freeze(operations[0]) == expected[0] # Flat list. assert rev_freeze(operations) == tuple(expected[::-1]) # Tree. assert (rev_freeze( (operations[1:5], operations[0], operations[5:])) == (tuple(expected[5:][::-1]), expected[0], tuple(expected[1:5][::-1]))) # Flattening after reversing is equivalent to reversing then flattening. t = (operations[1:5], operations[0], operations[5:]) assert (tuple(ops.flatten_op_tree(rev_freeze(t))) == tuple( rev_freeze(ops.flatten_op_tree(t))))
def test_init(): r = ScheduledOperation(time=Timestamp(picos=5), duration=Duration(picos=7), operation=ops.Operation(ops.H, [ops.NamedQubit('a')])) assert r.time == Timestamp(picos=5) assert r.duration == Duration(picos=7) assert r.operation == ops.Operation(ops.H, [ops.NamedQubit('a')])
def test_validate_operation_supported_gate(): d = square_device(3, 3) class MyGate(ops.Gate): pass d.validate_operation(ops.Operation(ExpZGate(), [XmonQubit(0, 0)])) with pytest.raises(ValueError): d.validate_operation(ops.Operation(MyGate, [XmonQubit(0, 0)]))
def test_validate_operation_adjacent_qubits(): d = square_device(3, 3) d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(1, 0)))) with pytest.raises(ValueError): d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(2, 0))))
def test_operation_eq(): g1 = ops.Gate() g2 = ops.Gate() r1 = [XmonQubit(1, 2)] r2 = [XmonQubit(3, 4)] r12 = r1 + r2 r21 = r2 + r1 eq = EqualsTester() eq.make_equality_pair(lambda: ops.Operation(g1, r1)) eq.make_equality_pair(lambda: ops.Operation(g2, r1)) eq.make_equality_pair(lambda: ops.Operation(g1, r2)) eq.make_equality_pair(lambda: ops.Operation(g1, r12)) eq.make_equality_pair(lambda: ops.Operation(g1, r21))
def test_validate_operation_existing_qubits(): d = square_device(3, 3, holes=[XmonQubit(1, 1)]) d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(1, 0)))) d.validate_operation(ops.Operation(ExpZGate(), (XmonQubit(0, 0), ))) with pytest.raises(ValueError): d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(0, 0), XmonQubit(-1, 0)))) with pytest.raises(ValueError): d.validate_operation(ops.Operation(ExpZGate(), (XmonQubit(-1, 0), ))) with pytest.raises(ValueError): d.validate_operation( ops.Operation(Exp11Gate(), (XmonQubit(1, 0), XmonQubit(1, 1))))
def _wrap_operation(op: ops.Operation, ext: extension.Extensions) -> ops.Operation: new_qubits = [_QCircuitQubit(e) for e in op.qubits] new_gate = ext.try_cast(op.gate, QCircuitDiagrammableGate) if new_gate is None: new_gate = fallback_qcircuit_extensions.cast(op.gate, QCircuitDiagrammableGate) return ops.Operation(_QCircuitGate(new_gate), new_qubits)
def test_operation_init(): q = XmonQubit(4, 5) g = ops.Gate() v = ops.Operation(g, (q,)) assert v.gate == g assert v.qubits == (q,)
def map_operation(self, operation: ops.Operation) -> ops.Operation: return ops.Operation(operation.gate, [self.qubit_map(q) for q in operation.qubits])
def test_validate_measurement_non_adjacent_qubits_ok(): d = square_device(3, 3) d.validate_operation( ops.Operation(XmonMeasurementGate(key=''), (XmonQubit(0, 0), XmonQubit(2, 0))))