Esempio n. 1
0
def test_time_evolution_info():
    # standard example
    gate = gates.TimeEvolution(-cmath.pi / 8, gates.QubitOperator("X0 X1 Y2"))
    gate_info = _rotations.time_evolution_info(gate)
    print(gate_info)
    assert (len(gate_info[0]) == 3)
    assert (gate_info[1] == "pi4" and gate_info[2] == cmath.pi / 4)

    # example with -angle
    gate = gates.TimeEvolution(-(2 * cmath.pi - cmath.pi / 4),
                               gates.QubitOperator("X0 X1 Y2"))
    gate_info = _rotations.time_evolution_info(gate)
    assert (gate_info[1] == "pi2"
            and gate_info[2] == 4 * cmath.pi - cmath.pi / 2)

    # does it throw errors as it is supposed to
    with pytest.raises(PermutationRuleDoesNotExist) as e:
        gate = gates.TimeEvolution(-cmath.pi / 16,
                                   gates.QubitOperator("X0 X1 Y2"))
        gate_info = _rotations.time_evolution_info(gate)
    return
Esempio n. 2
0
 def TimeEvolution(self, cmd):
     """
     Handles the basis transformation for TimeEvolution operators
     """
     assert (isinstance(cmd.gate.hamiltonian, gates.QubitOperator))
     bases = list(cmd.gate.hamiltonian.terms.keys())[0]
     self.rotation(bases, cmd)
     new_bases = tuple(((basis[0], "Z") for basis in bases))
     new_cmd = gates.TimeEvolution(
         cmd.gate.time,
         gates.QubitOperator(new_bases)).generate_command(cmd.qubits[0])
     self.send([new_cmd])
     self.dagger_rotation(bases, cmd)
def test_multirotation():
	commands = GetCommand()
	eng = projectq.MainEngine(engine_list=[commands])
	qubit1 = eng.allocate_qubit()
	qubit2 = eng.allocate_qubit()

	gates.CNOT | (qubit1, qubit2)
	gates.TimeEvolution(-1, gates.QubitOperator("X0 Z1")) | Qureg(qubit1 + qubit2)
	cmd2_info = [[(0,"X"), (1,"Z")], "pi4", 1]

	permute_cnot(commands.commands[0], commands.commands[1], cmd2_info)

	print(cmd2_info)
	assert(len(cmd2_info[0])==2)
	assert(cmd2_info[0][0][1] == "Y" and cmd2_info[0][1][1] == "Y")
Esempio n. 4
0
    def send_time_evolution(self, cmd):
        """
        Handles the basis transformation for TimeEvolution operators
        """
        assert(isinstance(cmd.gate.hamiltonian, gates.QubitOperator))

        # rotate the existing bases
        bases = list(cmd.gate.hamiltonian.terms.keys())[0]
        self.send_rotation(bases, cmd)

        # transform all the basis in Z
        new_bases = tuple(((basis[0],"Z") for basis in bases))
        new_cmd = gates.TimeEvolution(cmd.gate.time, gates.QubitOperator(new_bases)).generate_command(cmd.qubits[0])
        self.send([new_cmd])

        # rotate back to original bases
        self.send_dagger_rotation(bases, cmd)
Esempio n. 5
0
    def _generate_gate(gate):
        """
		Translates from the symbolic description of gates back into a projectq
		gate using the dictionary below.
		"""
        if (len(gate[0]) == 1):  # single qubit gate
            if (gate[0][0][1] in _GATE_FROM_INFO):
                return _GATE_FROM_INFO[gate[0][0][1]](gate[2])
        else:  # multiqubit gate
            # check for identities
            new_info = tuple((i for i in gate[0] if not i[1] == "I"))
            # TimeEvolution operators have a different prefactor
            # from rotations thus the division by -2
            return gates.TimeEvolution(-gate[2] / 2.,
                                       gates.QubitOperator(new_info))
        raise PermutationRuleDoesNotExist(
            """Cannot create rotation gate from the
			provided information.""")
Esempio n. 6
0
def test_gate_to_info():
    gate = gates.Rx(cmath.pi / 4)
    info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate)
    assert (info[:2] == [[(0, "X")], "pi4"])
    assert (abs(info[2] - (cmath.pi / 4)) < _PRECISION)

    gate = gates.Ry(cmath.pi / 2)
    info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate)
    assert (info[:2] == [[(0, "Y")], "pi2"])
    assert (abs(info[2] - (cmath.pi / 2)) < _PRECISION)

    gate = gates.Rz(cmath.pi)
    info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate)
    assert (info[:2] == [[(0, "Z")], "pi"])
    assert (abs(info[2] - (cmath.pi)) < _PRECISION)

    gate = gates.TimeEvolution(-cmath.pi / 8, gates.QubitOperator("X0 X1 Y2"))
    info = _rotations._ROTATION_GATE_TO_INFO[type(gate)](gate)
    assert (info[:2] == [[(0, "X"), (1, "X"), (2, "Y")], "pi4"])
    assert (abs(info[2] - (cmath.pi / 4)) < _PRECISION)
    return
def test_simple2():
    eng = projectq.MainEngine(
        engine_list=[], backend=SimpleExporter(output="test_simple_case2"))
    qubit1 = eng.allocate_qubit()
    qubit2 = eng.allocate_qubit()
    gates.TimeEvolution(-cmath.pi / 8,
                        gates.QubitOperator("Y0 X1")) | qubit1 + qubit2

    eng.flush()
    del qubit1
    del qubit2

    with open("test_simple_case2") as fin:
        line = fin.readline()
        line = line.strip()
        assert (line == "INIT 2")

        line = fin.readline()
        line = line.strip()
        assert (line == "H 0")

        line = fin.readline()
        line = line.strip()
        assert (line == "S 0")

        line = fin.readline()
        line = line.strip()
        assert (line == "H 1")

        line = fin.readline()
        line = line.strip()
        assert (line == "NEED A")

        line = fin.readline()
        line = line.strip()
        assert (line == "MZZ A 0 1")

        line = fin.readline()
        line = line.strip()
        assert (line == "MX A")

        line = fin.readline()
        line = line.strip()
        assert (line == "S ANCILLA")

        line = fin.readline()
        line = line.strip()
        assert (line == "MXX ANCILLA 0 1")

        line = fin.readline()
        line = line.strip()
        assert (line == "S 0")

        line = fin.readline()
        line = line.strip()
        assert (line == "H 0")

        line = fin.readline()
        line = line.strip()
        assert (line == "H 1")

    os.remove("test_simple_case2")
Esempio n. 8
0
 def expb(t, qureg):
     hamiltonian = reduce(
         lambda x, y: x + y,
         [ops.QubitOperator('X%d' % i) for i in range(len(qureg))])
     ops.TimeEvolution(t, hamiltonian=hamiltonian) | qureg
Esempio n. 9
0
 def expb(t, qureg):
     hamiltonian = reduce(lambda x,y:x+y, [w*ops.QubitOperator(' '.join(['Z%d'%i for i in zstring])) \
                                           for w, zstring in clause_list])
     ops.TimeEvolution(t, hamiltonian=hamiltonian) | qureg
Esempio n. 10
0
 def expb(t, qureg):
     hamiltonian = reduce(
         lambda x, y: x + y,
         [single_hamiltonian(clause) for clause in clause_list])
     ops.TimeEvolution(t, hamiltonian=hamiltonian) | qureg
Esempio n. 11
0
def test_TimeEvolution(benchmark, nbit):
    benchmark.group = "TimeEvolution"
    run_bench(benchmark, ops.TimeEvolution(1.0, ising_hamiltonian(nbit)), None,
              nbit)