def test_hash_function_implemented(): assert hash(AddConstant(3)) == hash(str(AddConstant(3))) assert hash(SubConstant(-3)) == hash(str(AddConstant(3))) assert hash(AddConstantModN(7, 4)) == hash(str(AddConstantModN(7, 4))) assert hash(SubConstantModN(7, 4)) == hash(str(AddConstantModN(-3, 4))) assert hash(MultiplyByConstantModN(3, 5)) == hash( MultiplyByConstantModN(3, 5))
def test_hash_function_implemented(): assert hash(AddConstant(3)) == hash(str(AddConstant(3))) assert hash(SubConstant(-3)) == hash(str(AddConstant(3))) assert hash(AddConstantModN(7, 4)) == hash(str(AddConstantModN(7, 4))) assert hash(SubConstantModN(7, 4)) == hash(str(AddConstantModN(-3, 4))) assert hash(MultiplyByConstantModN(3, 5)) == hash( str(MultiplyByConstantModN(3, 5))) assert hash(AddQuantum) == hash(str(AddQuantum)) assert hash(SubtractQuantum) == hash(str(SubtractQuantum)) assert hash(ComparatorQuantum) == hash(str(ComparatorQuantum)) assert hash(DivideQuantum) == hash(str(DivideQuantum)) assert hash(MultiplyQuantum) == hash(str(MultiplyQuantum))
def test_modadder(eng): qureg = eng.allocate_qureg(4) init(eng, qureg, 4) AddConstantModN(3, 6) | qureg assert 1.0 == pytest.approx(abs(eng.backend.cheat()[1][1])) init(eng, qureg, 1) # reset init(eng, qureg, 7) AddConstantModN(10, 13) | qureg assert 1.0 == pytest.approx(abs(eng.backend.cheat()[1][4])) All(Measure) | qureg
def test_modadder(): sim = Simulator() eng = MainEngine(sim, [AutoReplacer(), InstructionFilter(no_math_emulation)]) qureg = eng.allocate_qureg(4) init(eng, qureg, 4) AddConstantModN(3, 6) | qureg assert 1. == pytest.approx(abs(sim.cheat()[1][1])) init(eng, qureg, 1) # reset init(eng, qureg, 7) AddConstantModN(10, 13) | qureg assert 1. == pytest.approx(abs(sim.cheat()[1][4])) Measure | qureg
def test_restriction(): engine_list = restrictedgateset.get_engine_list( one_qubit_gates=(Rz, H), two_qubit_gates=(CNOT, AddConstant, Swap), other_gates=(Toffoli, AddConstantModN, MultiplyByConstantModN(2, 8)), ) backend = DummyEngine(save_commands=True) eng = projectq.MainEngine(backend, engine_list, verbose=True) qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() eng.flush() CNOT | (qubit1, qubit2) H | qubit1 with Control(eng, qubit2): Rz(0.2) | qubit1 Measure | qubit1 AddConstant(1) | (qubit1 + qubit2) AddConstantModN(1, 9) | (qubit1 + qubit2 + qubit3) Toffoli | (qubit1 + qubit2, qubit3) Swap | (qubit1, qubit2) MultiplyByConstantModN(2, 8) | qubit1 + qubit2 + qubit3 TimeEvolution(0.5, QubitOperator("X0 Y1 Z2")) | qubit1 + qubit2 + qubit3 QFT | qubit1 + qubit2 + qubit3 Rx(0.1) | (qubit1) MultiplyByConstantModN(2, 9) | qubit1 + qubit2 + qubit3 eng.flush() assert backend.received_commands[4].gate == X assert len(backend.received_commands[4].control_qubits) == 1 assert backend.received_commands[5].gate == H assert backend.received_commands[6].gate == Rz(0.1) assert backend.received_commands[10].gate == Measure assert backend.received_commands[11].gate == AddConstant(1) assert backend.received_commands[12].gate == AddConstantModN(1, 9) assert backend.received_commands[13].gate == X assert len(backend.received_commands[13].control_qubits) == 2 assert backend.received_commands[14].gate == Swap assert backend.received_commands[15].gate == MultiplyByConstantModN(2, 8) for cmd in backend.received_commands[16:]: assert cmd.gate != QFT assert not isinstance(cmd.gate, Rx) assert not isinstance(cmd.gate, MultiplyByConstantModN) assert not isinstance(cmd.gate, TimeEvolution)
def run_simulation(sim): eng = MainEngine(sim, []) quint = eng.allocate_qureg(5) AddConstant(3) | quint All(Measure) | quint eng.flush() results[0].append([int(qb) for qb in quint]) AddConstantModN(4, 5) | quint All(Measure) | quint eng.flush() results[1].append([int(qb) for qb in quint]) MultiplyByConstantModN(15, 16) | quint All(Measure) | quint eng.flush() results[2].append([int(qb) for qb in quint])
def test_addconstantmodn(): assert AddConstantModN(3, 4) == AddConstantModN(3, 4) assert not AddConstantModN(3, 4) == AddConstantModN(4, 4) assert not AddConstantModN(3, 5) == AddConstantModN(3, 4) assert AddConstantModN(7, 4) != AddConstantModN(3, 4) assert AddConstantModN(3, 5) != AddConstantModN(3, 4) assert str(AddConstantModN(3, 4)) == "AddConstantModN(3, 4)"
def eng(): return MainEngine( backend=Simulator(), engine_list=[ AutoReplacer(rule_set), InstructionFilter(no_math_emulation) ], ) rule_set = DecompositionRuleSet( modules=[projectq.libs.math, qft2crandhadamard, swap2cnot]) @pytest.mark.parametrize('gate', (AddConstantModN(-1, 6), MultiplyByConstantModN( -1, 6), MultiplyByConstantModN(4, 4)), ids=str) def test_invalid(eng, gate): qureg = eng.allocate_qureg(4) init(eng, qureg, 4) with pytest.raises(ValueError): gate | qureg eng.flush() def test_adder(eng): qureg = eng.allocate_qureg(4) init(eng, qureg, 4)