def test_resource_counter_measurement(): eng = MainEngine(ResourceCounter(), []) qb1 = WeakQubitRef(engine=eng, idx=1) qb2 = WeakQubitRef(engine=eng, idx=2) cmd0 = Command(engine=eng, gate=Allocate, qubits=([qb1],)) cmd1 = Command(engine=eng, gate=Measure, qubits=([qb1],), controls=[], tags=[LogicalQubitIDTag(2)]) with pytest.raises(NotYetMeasuredError): int(qb1) with pytest.raises(NotYetMeasuredError): int(qb2) eng.send([cmd0, cmd1]) eng.flush() with pytest.raises(NotYetMeasuredError): int(qb1) assert int(qb2) == 0
def test_cannot_reallocate_same_qubit(): engine = MainEngine( Simulator(), engine_list=[BoundedQubitMapper(1)], verbose=True, ) qureg = engine.allocate_qubit() qubit = qureg[0] qubit_id = qubit.id with pytest.raises(RuntimeError) as excinfo: allocate_cmd = Command( engine=engine, gate=AllocateQubitGate(), qubits=([WeakQubitRef(engine=engine, idx=qubit_id)], ), tags=[LogicalQubitIDTag(qubit_id)], ) engine.send([allocate_cmd]) assert str(excinfo.value) == "Qubit with id 0 has already been allocated!"
def test_cannot_deallocate_same_qubit(): mapper = BoundedQubitMapper(1) engine = MainEngine( Simulator(), engine_list=[mapper], verbose=True, ) qureg = engine.allocate_qubit() qubit_id = qureg[0].id engine.deallocate_qubit(qureg[0]) with pytest.raises(RuntimeError) as excinfo: deallocate_cmd = Command( engine=engine, gate=DeallocateQubitGate(), qubits=([WeakQubitRef(engine=engine, idx=qubit_id)], ), tags=[LogicalQubitIDTag(qubit_id)], ) engine.send([deallocate_cmd]) assert str(excinfo.value ) == "Cannot deallocate a qubit that is not already allocated!"
def test_cannot_deallocate_unknown_qubit(): engine = MainEngine( Simulator(), engine_list=[BoundedQubitMapper(1)], verbose=True, ) qureg = engine.allocate_qubit() with pytest.raises(RuntimeError) as excinfo: deallocate_cmd = Command( engine=engine, gate=DeallocateQubitGate(), qubits=([WeakQubitRef(engine=engine, idx=1)], ), tags=[LogicalQubitIDTag(1)], ) engine.send([deallocate_cmd]) assert str(excinfo.value ) == "Cannot deallocate a qubit that is not already allocated!" # but we can still deallocate an already allocated one engine.deallocate_qubit(qureg[0]) del qureg del engine
def test_unitary_measure_mapped_qubit(): eng = MainEngine(UnitarySimulator()) qb1 = WeakQubitRef(engine=eng, idx=1) qb2 = WeakQubitRef(engine=eng, idx=2) cmd0 = Command(engine=eng, gate=Allocate, qubits=([qb1], )) cmd1 = Command(engine=eng, gate=X, qubits=([qb1], )) cmd2 = Command( engine=eng, gate=Measure, qubits=([qb1], ), controls=[], tags=[LogicalQubitIDTag(2)], ) with pytest.raises(NotYetMeasuredError): int(qb1) with pytest.raises(NotYetMeasuredError): int(qb2) eng.send([cmd0, cmd1]) eng.flush() eng.send([cmd2]) with pytest.raises(NotYetMeasuredError): int(qb1) assert int(qb2) == 1