def test_controlled_gate_or(): saving_backend = DummyEngine(save_commands=True) main_engine = MainEngine(backend=saving_backend, engine_list=[DummyEngine()]) gate = Rx(0.6) qubit0 = Qubit(main_engine, 0) qubit1 = Qubit(main_engine, 1) qubit2 = Qubit(main_engine, 2) qubit3 = Qubit(main_engine, 3) expected_cmd = Command(main_engine, gate, ([qubit3], )) expected_cmd.add_control_qubits([qubit0, qubit1, qubit2]) received_commands = [] # Option 1: _metagates.ControlledGate(gate, 3) | ([qubit1], [qubit0], [qubit2 ], [qubit3]) # Option 2: _metagates.ControlledGate(gate, 3) | (qubit1, qubit0, qubit2, qubit3) # Option 3: _metagates.ControlledGate(gate, 3) | ([qubit1, qubit0], qubit2, qubit3) # Option 4: _metagates.ControlledGate(gate, 3) | (qubit1, [qubit0, qubit2], qubit3) # Wrong option 5: with pytest.raises(_metagates.ControlQubitError): _metagates.ControlledGate(gate, 3) | (qubit1, [qubit0, qubit2, qubit3]) # Remove Allocate and Deallocate gates for cmd in saving_backend.received_commands: if not (isinstance(cmd.gate, FastForwardingGate) or isinstance(cmd.gate, ClassicalInstructionGate)): received_commands.append(cmd) assert len(received_commands) == 4 for cmd in received_commands: assert cmd == expected_cmd
def test_controlled_gate_comparison(): gate1 = _metagates.ControlledGate(Y, 1) gate2 = _metagates.ControlledGate(Y, 1) gate3 = _metagates.ControlledGate(T, 1) gate4 = _metagates.ControlledGate(Y, 2) assert gate1 == gate2 assert not gate1 == gate3 assert gate1 != gate4
def test_controlled_gate_init(): one_control = _metagates.ControlledGate(Y, 1) two_control = _metagates.ControlledGate(Y, 2) three_control = _metagates.ControlledGate(one_control, 2) assert one_control._gate == Y assert one_control._n == 1 assert two_control._gate == Y assert two_control._n == 2 assert three_control._gate == Y assert three_control._n == 3
def test_controlled_gate_empty_controls(): rec = DummyEngine(save_commands=True) eng = MainEngine(backend=rec, engine_list=[]) a = eng.allocate_qureg(1) _metagates.ControlledGate(Y, 0) | ((), a) assert rec.received_commands[-1] == Command(eng, Y, [a])
def test_controlled_gate_str(): one_control = _metagates.ControlledGate(Y, 2) assert str(one_control) == "CC" + str(Y)
def test_c(): expected = _metagates.ControlledGate(Y, 2) assert _metagates.C(Y, 2) == expected
def test_controlled_gate_get_inverse(): one_control = _metagates.ControlledGate(Rx(0.5), 1) expected = _metagates.ControlledGate(Rx(-0.5 + 4 * math.pi), 1) assert one_control.get_inverse() == expected