def test_control(): backend = DummyEngine(save_commands=True) eng = MainEngine(backend=backend, engine_list=[DummyEngine()]) qureg = eng.allocate_qureg(2) with _control.Control(eng, qureg): qubit = eng.allocate_qubit() with Compute(eng): Rx(0.5) | qubit H | qubit Uncompute(eng) with _control.Control(eng, qureg[0]): H | qubit eng.flush() assert len(backend.received_commands) == 8 assert len(backend.received_commands[0].control_qubits) == 0 assert len(backend.received_commands[1].control_qubits) == 0 assert len(backend.received_commands[2].control_qubits) == 0 assert len(backend.received_commands[3].control_qubits) == 0 assert len(backend.received_commands[4].control_qubits) == 2 assert len(backend.received_commands[5].control_qubits) == 0 assert len(backend.received_commands[6].control_qubits) == 1 assert len(backend.received_commands[7].control_qubits) == 0 assert backend.received_commands[4].control_qubits[0].id == qureg[0].id assert backend.received_commands[4].control_qubits[1].id == qureg[1].id assert backend.received_commands[6].control_qubits[0].id == qureg[0].id
def test_control_state_contradiction(): backend = DummyEngine(save_commands=True) eng = MainEngine(backend=backend, engine_list=[DummyEngine()]) qureg = eng.allocate_qureg(1) with pytest.raises(IncompatibleControlState): with _control.Control(eng, qureg[0], '0'): qubit = eng.allocate_qubit() with _control.Control(eng, qureg[0], '1'): H | qubit eng.flush()
def test_control_state(): backend = DummyEngine(save_commands=True) eng = MainEngine(backend=backend, engine_list=[DummyEngine()]) qureg = eng.allocate_qureg(3) xreg = eng.allocate_qureg(3) X | qureg[1] with _control.Control(eng, qureg[0], '0'): with Compute(eng): X | xreg[0] X | xreg[1] Uncompute(eng) with _control.Control(eng, qureg[1:], 2): X | xreg[2] eng.flush() assert len(backend.received_commands) == 6 + 5 + 1 assert len(backend.received_commands[0].control_qubits) == 0 assert len(backend.received_commands[1].control_qubits) == 0 assert len(backend.received_commands[2].control_qubits) == 0 assert len(backend.received_commands[3].control_qubits) == 0 assert len(backend.received_commands[4].control_qubits) == 0 assert len(backend.received_commands[5].control_qubits) == 0 assert len(backend.received_commands[6].control_qubits) == 0 assert len(backend.received_commands[7].control_qubits) == 0 assert len(backend.received_commands[8].control_qubits) == 1 assert len(backend.received_commands[9].control_qubits) == 0 assert len(backend.received_commands[10].control_qubits) == 2 assert len(backend.received_commands[11].control_qubits) == 0 assert backend.received_commands[8].control_qubits[0].id == qureg[0].id assert backend.received_commands[8].control_state == '0' assert backend.received_commands[10].control_qubits[0].id == qureg[1].id assert backend.received_commands[10].control_qubits[1].id == qureg[2].id assert backend.received_commands[10].control_state == '01' assert _control.has_negative_control(backend.received_commands[8]) assert _control.has_negative_control(backend.received_commands[10])