コード例 #1
0
def test_single_qubit_basis_transfrom():
    '''
    Testing whether transformations using the binary form 
    and the transformation through direct computation agree
    '''
    H, n_qubits, binary_sol, coeff_sol = prepare_test_hamiltonian()

    single_qub_H, old_basis, new_basis = BinaryHamiltonian.init_from_qubit_hamiltonian(
        H).single_qubit_form()

    H_brute_force = brute_force_transformation(H, old_basis, new_basis)

    assert (equal_qubit_hamiltonian(single_qub_H.to_qubit_hamiltonian(),
                                    H_brute_force))

    H = -1.0 * paulis.X(0) * paulis.X(1) * paulis.X(2) + 2.0 * paulis.Y(
        0) * paulis.Y(1)

    single_qub_H, old_basis, new_basis = BinaryHamiltonian.init_from_qubit_hamiltonian(
        H).single_qubit_form()

    H_brute_force = brute_force_transformation(H, old_basis, new_basis)

    assert (equal_qubit_hamiltonian(single_qub_H.to_qubit_hamiltonian(),
                                    H_brute_force))
コード例 #2
0
def test_simple_arithmetic():
    qubit = random.randint(0, 5)
    primitives = [paulis.X, paulis.Y, paulis.Z]
    assert (paulis.X(qubit).conjugate() == paulis.X(qubit))
    assert (paulis.Y(qubit).conjugate() == -1 * paulis.Y(qubit))
    assert (paulis.Z(qubit).conjugate() == paulis.Z(qubit))
    assert (paulis.X(qubit).transpose() == paulis.X(qubit))
    assert (paulis.Y(qubit).transpose() == -1 * paulis.Y(qubit))
    assert (paulis.Z(qubit).transpose() == paulis.Z(qubit))
    for P in primitives:
        assert (P(qubit) * P(qubit) == QubitHamiltonian(1.0))
        n = random.randint(0, 10)
        nP = QubitHamiltonian.zero()
        for i in range(n):
            nP += P(qubit)
        assert (n * P(qubit) == nP)

    for i, Pi in enumerate(primitives):
        i1 = (i + 1) % 3
        i2 = (i + 2) % 3
        assert (Pi(qubit) * primitives[i1](qubit) == 1j *
                primitives[i2](qubit))
        assert (primitives[i1](qubit) * Pi(qubit) == -1j *
                primitives[i2](qubit))

        for qubit2 in random.randint(6, 10, 5):
            if qubit2 == qubit: continue
            P = primitives[random.randint(0, 2)]
            assert (Pi(qubit) * primitives[i1](qubit) * P(qubit2) == 1j *
                    primitives[i2](qubit) * P(qubit2))
            assert (P(qubit2) * primitives[i1](qubit) * Pi(qubit) == -1j *
                    P(qubit2) * primitives[i2](qubit))
コード例 #3
0
def test_paulistring_conversion():
    X1 = QubitHamiltonian.from_string("X0", openfermion_format=True)
    X2 = paulis.X(0)
    keys = [i for i in X2.keys()]
    pwx = PauliString.from_openfermion(key=keys[0], coeff=X2[keys[0]])
    X3 = QubitHamiltonian.from_paulistrings(pwx)
    assert (X1 == X2)
    assert (X2 == X3)

    H = paulis.X(0) * paulis.Y(1) * paulis.Z(2) + paulis.X(3) * paulis.Y(
        4) * paulis.Z(5)
    PS = []
    for key, value in H.items():
        PS.append(PauliString.from_openfermion(key, value))
    PS2 = H.paulistrings
    assert (PS == PS2)

    H = make_random_pauliword(complex=True)
    for i in range(5):
        H += make_random_pauliword(complex=True)
    PS = []
    for key, value in H.items():
        PS.append(PauliString.from_openfermion(key, value))
    PS2 = H.paulistrings
    assert (PS == PS2)
コード例 #4
0
def test_dagger():
    assert (paulis.X(0).dagger() == paulis.X(0))
    assert (paulis.Y(0).dagger() == paulis.Y(0))
    assert (paulis.Z(0).dagger() == paulis.Z(0))

    for repeat in range(10):
        string = make_random_pauliword(complex=False)
        assert (string.dagger() == string)
        assert ((1j * string).dagger() == -1j * string)
コード例 #5
0
def prepare_test_hamiltonian():
    '''
    Return a test hamiltonian and its solution
    '''
    H = -1.0 * paulis.Z(0) * paulis.Z(1) - 0.5 * paulis.Y(0) * paulis.Y(
        1) + 0.1 * paulis.X(0) * paulis.X(1) + 0.2 * paulis.Z(2)
    coeff_sol = np.array([-1.0, -0.5, 0.1, 0.2])
    binary_sol = np.array([[0, 0, 0, 1, 1, 0], [1, 1, 0, 1, 1, 0],
                           [1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1]])

    return H, H.n_qubits, binary_sol, coeff_sol
コード例 #6
0
def test_matrix_form():
    H = -1.0 * paulis.Z(0) -1.0 * paulis.Z(1) + 0.1 * paulis.X(0)*paulis.X(1) 
    Hm= H.to_matrix()
    assert (Hm[0,0] == -2.0)
    assert (Hm[0,3] == 0.10)
    assert (Hm[1,2] == 0.10)

    Hm2 = (H + paulis.Z(2)).to_matrix()
    Hm2p = kron(Hm, eye(2, dtype=Hm2.dtype)) + kron(eye(len(Hm), dtype=Hm2.dtype), paulis.Z(0).to_matrix())
    assert allclose(Hm2 , Hm2p)

    Hm3 = (H * paulis.Z(2)).to_matrix()
    Hm3p = kron(Hm, paulis.Z(0).to_matrix())
    assert allclose(Hm3 , Hm3p)
コード例 #7
0
ファイル: _gates_impl.py プロジェクト: sunchong137/tequila
    def assign_generator(axis, qubits):
        if axis == 0:
            return sum(paulis.X(q) for q in qubits)
        if axis == 1:
            return sum(paulis.Y(q) for q in qubits)

        return sum(paulis.Z(q) for q in qubits)
コード例 #8
0
ファイル: test_gradient.py プロジェクト: silky/tequila
def test_gradient_deep_H(simulator, power, controls):
    if controls > 2 and simulator == "qiskit":
        # does not work yet
        return
    qubit = 0
    angle = Variable(name="angle")
    variables = {angle: power}
    control = [i for i in range(1, controls + 1)]
    H = paulis.X(qubit=qubit)

    U = gates.X(target=control) + gates.H(
        target=qubit, control=control, power=angle)

    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    assert (numpy.isclose(E,
                          -numpy.cos(angle(variables) * (numpy.pi)) / 2 + 0.5,
                          atol=1.e-4))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)

    assert (numpy.isclose(dE,
                          numpy.pi * numpy.sin(angle(variables) * (numpy.pi)) /
                          2,
                          atol=1.e-4))
コード例 #9
0
ファイル: test_gradient.py プロジェクト: silky/tequila
def test_gradient_H(simulator, power, controlled):
    qubit = 0
    control = 1
    angle = Variable(name="angle")
    variables = {angle: power}

    H = paulis.X(qubit=qubit)
    if not controlled:
        U = gates.H(target=qubit, power=angle)
    else:
        U = gates.X(target=control) + gates.H(
            target=qubit, control=control, power=angle)

    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    assert (numpy.isclose(E,
                          -numpy.cos(angle(variables) * (numpy.pi)) / 2 + 0.5,
                          atol=1.e-4))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)

    assert (numpy.isclose(dE,
                          numpy.pi * numpy.sin(angle(variables) * (numpy.pi)) /
                          2,
                          atol=1.e-4))
コード例 #10
0
ファイル: test_gradient.py プロジェクト: silky/tequila
def test_gradient_UY_HX(simulator, angle_value, controlled, silent=True):
    # case X Y
    # U = cos(angle/2) + sin(-angle/2)*i*Y
    # <0|Ud H U |0> = cos^2(angle/2)*<0|X|0>
    # + sin^2(-angle/2) <0|YXY|0>
    # + cos(angle/2)*sin(angle/2)*i<0|XY|0>
    # + sin(-angle/2)*cos(angle/2)*(-i) <0|YX|0>
    # = cos^2*0 + sin^2*0 + cos*sin*i(<0|[XY,YX]|0>)
    # = 0.5*sin(-angle)*i <0|[XY,YX]|0> = -0.5*sin(angle)*i * 2 i <0|Z|0>
    # = sin(angle)

    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.X(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.Ry(
            target=qubit, control=control, angle=angle)
    else:
        U = gates.X(target=qubit) + gates.X(target=qubit) + gates.Ry(
            target=qubit, angle=angle)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    print("O={type}".format(type=type(O)))
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)
    assert (numpy.isclose(E, numpy.sin(angle(variables)), atol=1.e-4))
    assert (numpy.isclose(dE, numpy.cos(angle(variables)), atol=1.e-4))
    if not silent:
        print("E         =", E)
        print("sin(angle)=", numpy.sin(angle()))
        print("dE        =", dE)
        print("cos(angle)=", numpy.cos(angle()))
コード例 #11
0
ファイル: qng.py プロジェクト: shukob/tequila
def get_generator(gate) -> paulis.QubitHamiltonian:
    """
    get the generator of a gaussian gate as a Qubit hamiltonian. Relies on the name of the gate.
    Parameters
    ----------
    gate: QGateImpl:
        QGateImpl object or inheritor thereof, with name corresponding to its generator in some fashion.

    Returns
    -------
    QubitHamiltonian:
        the generator of the gate acting, on the gate's target.

    """

    if gate.name.lower() == 'rx':
        gen = paulis.X(gate.target[0])
    elif gate.name.lower() == 'ry':
        gen = paulis.Y(gate.target[0])
    elif gate.name.lower() == 'rz':
        gen = paulis.Z(gate.target[0])
    elif gate.name.lower() == 'phase':
        gen = paulis.Qm(gate.target[0])
    else:
        print(gate.name.lower())
        raise TequilaException(
            'cant get the generator of a non Gaussian gate, you fool!')
    return gen
コード例 #12
0
def test_mixed_power(
    simulator,
    value1=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0)),
    value2=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(
        target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    H2 = paulis.Y(qubit=qubit)
    U2 = gates.X(target=control) + gates.Rx(
        target=qubit, control=control, angle=angle2)
    e2 = ExpectationValue(U=U2, H=H2)
    added = e1**e2
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator)
    en2 = simulate(e2, variables=variables, backend=simulator)
    an1 = np.sin(angle1(variables=variables))
    an2 = -np.sin(angle2(variables=variables))
    assert np.isclose(val, en1**en2, atol=1.e-4)
    assert np.isclose(val, an1**an2, atol=1.e-4)
コード例 #13
0
def test_hadamard(qubit, init):
    gate = gates.H(target=qubit)
    iwfn = QubitWaveFunction.from_int(i=init, n_qubits=qubit + 1)
    wfn = simulate(gate, initial_state=init)
    test = 1.0 / numpy.sqrt(2) * (iwfn.apply_qubitoperator(paulis.Z(qubit)) +
                                  iwfn.apply_qubitoperator(paulis.X(qubit)))
    assert (wfn.isclose(test))
コード例 #14
0
def test_phase_damp(simulator, p):

    qubit = 0
    H = paulis.X(qubit)
    U = gates.H(target=qubit)
    O = ExpectationValue(U=U, H=H)
    NM = PhaseDamp(p, 1)
    E = simulate(O, backend=simulator, samples=1, noise=NM)
コード例 #15
0
ファイル: test_noise.py プロジェクト: philipp-q/tequila-1
def test_phase_flip(simulator, p):
    qubit = 0
    H = paulis.X(qubit)
    U = gates.H(target=qubit)
    O = ExpectationValue(U=U, H=H)
    NM = PhaseFlip(p, 1)
    E = simulate(O, backend=simulator, samples=1000, noise=NM)
    assert (numpy.isclose(E, 1.0 - 2 * p, atol=1.e-1))
コード例 #16
0
def test_initialization():
    H = paulis.I()
    for i in range(10):
        H += paulis.pauli(qubit=numpy.random.randint(0,5,3), type=numpy.random.choice(["X", "Y", "Z"],1))

    for H1 in [H, paulis.I(), paulis.Zero(), paulis.X(0), paulis.Y(1), 1.234*paulis.Z(2)]:
        string = str(H1)
        ofstring = str(H1.to_openfermion())
        H2 = QubitHamiltonian.from_string(string=string)
        assert H1 == H2
        H3 = QubitHamiltonian.from_string(string=ofstring, openfermion_format=True)
        assert H1 == H3
コード例 #17
0
def test_commuting_groups():
    '''
    Testing whether the partitioning gives commuting parts
    '''
    H, _, _, _ = prepare_test_hamiltonian()
    H = H + paulis.X(0) + paulis.Y(0)
    H = BinaryHamiltonian.init_from_qubit_hamiltonian(H)

    commuting_parts = H.commuting_groups()

    for part in commuting_parts:
        assert part.is_commuting()
コード例 #18
0
def test_compilation(backend):
    U = gates.X(target=[0, 1, 2, 3, 4, 5])
    for i in range(10):
        U += gates.Ry(angle=(i, ), target=numpy.random.randint(0, 5, 1)[0])
    U += gates.CZ(0, 1) + gates.CNOT(1, 2) + gates.CZ(2, 3) + gates.CNOT(
        3, 4) + gates.CZ(5, 6)
    H = paulis.X(0) + paulis.X(1) + paulis.X(2) + paulis.X(3) + paulis.X(
        4) + paulis.X(5)
    H += paulis.Z(0) + paulis.Z(1) + paulis.Z(2) + paulis.Z(3) + paulis.Z(
        4) + paulis.Z(5)
    E = ExpectationValue(H=H, U=U)

    randvals = numpy.random.uniform(0.0, 2.0, 10)
    variables = {(i, ): randvals[i] for i in range(10)}
    e0 = simulate(E, variables=variables, backend=backend)

    E2 = E * E
    for i in range(99):
        E2 += E * E

    compiled = tq.compile(E2, variables=variables, backend=backend)
    e2 = compiled(variables=variables)
    assert (E2.count_expectationvalues(unique=True) == 1)
    assert (compiled.count_expectationvalues(unique=True) == 1)
    assert numpy.isclose(100 * e0**2, e2)
コード例 #19
0
def test_l_division(simulator, value=numpy.random.uniform(0.0, 2.0*numpy.pi, 1)[0]):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = e1 / 2
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator) / 2
    an1 = np.sin(value) / 2.
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
コード例 #20
0
def get_generator(gate):
    if gate.name.lower() == 'rx':
        gen = paulis.X(gate.target[0])
    elif gate.name.lower() == 'ry':
        gen = paulis.Y(gate.target[0])
    elif gate.name.lower() == 'rz':
        gen = paulis.Z(gate.target[0])
    elif gate.name.lower() == 'phase':
        gen = paulis.Qm(gate.target[0])
    else:
        print(gate.name.lower())
        raise TequilaException(
            'cant get the generator of a non Gaussian gate, you fool!')
    return gen
コード例 #21
0
def test_r_power(simulator, value=numpy.random.uniform(0.1, 1.9*numpy.pi, 1)[0]):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = 2 ** e1
    val = simulate(added, variables=variables, backend=simulator)
    en1 = 2 ** simulate(e1, variables=variables, backend=simulator)
    an1 = 2. ** np.sin(angle1(variables=variables))
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
コード例 #22
0
def test_l_addition(simulator, value=(numpy.random.randint(0, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    variables = {angle1: value}
    qubit = 0
    control = 1
    H1 = paulis.X(qubit=qubit)
    U1 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle1)
    e1 = ExpectationValue(U=U1, H=H1)
    added = e1 + 1
    val = simulate(added, variables=variables, backend=simulator)
    en1 = simulate(e1, variables=variables, backend=simulator) + 1.
    an1 = np.sin(angle1(variables=variables)) + 1.
    assert np.isclose(val, en1, atol=1.e-4)
    assert np.isclose(val, an1, atol=1.e-4)
コード例 #23
0
def test_special_operators():
    # sigma+ sigma- as well as Q+ and Q-
    assert (paulis.Sp(0) * paulis.Sp(0) == QubitHamiltonian.zero())
    assert (paulis.Sm(0) * paulis.Sm(0) == QubitHamiltonian.zero())

    assert (paulis.Qp(0) * paulis.Qp(0) == paulis.Qp(0))
    assert (paulis.Qm(0) * paulis.Qm(0) == paulis.Qm(0))
    assert (paulis.Qp(0) * paulis.Qm(0) == QubitHamiltonian.zero())
    assert (paulis.Qm(0) * paulis.Qp(0) == QubitHamiltonian.zero())

    assert (paulis.Sp(0) * paulis.Sm(0) == paulis.Qp(0))
    assert (paulis.Sm(0) * paulis.Sp(0) == paulis.Qm(0))

    assert (paulis.Sp(0) + paulis.Sm(0) == paulis.X(0))
    assert (paulis.Qp(0) + paulis.Qm(0) == paulis.I(0))
コード例 #24
0
def test_heterogeneous_operations_l(simulator, op, value1=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0)),
                                    value2=(numpy.random.randint(1, 1000) / 1000.0 * (numpy.pi / 2.0))):
    angle1 = Variable(name="angle1")
    angle2 = Variable(name="angle2")
    variables = {angle1: value1, angle2: value2}
    qubit = 0
    control = 1
    H2 = paulis.X(qubit=qubit)
    U2 = gates.X(target=control) + gates.Ry(target=qubit, control=control, angle=angle2)
    e2 = ExpectationValue(U=U2, H=H2)
    added = Objective(args=[angle1, e2.args[0]], transformation=op)
    val = simulate(added, variables=variables, backend=simulator)
    en2 = simulate(e2, variables=variables, backend=simulator)
    an1 = angle1(variables=variables)
    an2 = np.sin(angle2(variables=variables))
    assert np.isclose(val, float(op(an1, en2)), atol=1.e-4)
    assert np.isclose(en2, an2, atol=1.e-4)
コード例 #25
0
def test_gradient_Y(simulator, power, controlled):
    if simulator != "cirq":
        return
    qubit = 0
    control = 1
    angle = Variable(name="angle")
    if controlled:
        U = gates.X(target=control) + gates.Y(target=qubit, power=angle, control=control)
    else:
        U = gates.Y(target=qubit, power=angle)
    angle = Variable(name="angle")
    variables = {angle: power}
    H = paulis.X(qubit=qubit)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable=angle)
    dE = simulate(dO, variables=variables, backend=simulator)
    assert (numpy.isclose(E, numpy.sin(angle(variables) * (numpy.pi)), atol=1.e-4))
    assert (numpy.isclose(dE, numpy.pi * numpy.cos(angle(variables) * (numpy.pi)), atol=1.e-4))
コード例 #26
0
def H(target: typing.Union[list, int],
      control: typing.Union[list, int] = None,
      power=None,
      angle=None,
      *args,
      **kwargs) -> QCircuit:
    """
    Notes
    ----------
    Hadamard gate

    Parameters
    ----------
    target
        int or list of int
    control
        int or list of int
    power
        numeric type (fixed exponent) or hashable type (parametrized exponent)
        angle
        similar to power, but will be interpreted as
        .. math::
           U(\\text{angle})=e^{-i\\frac{angle}{2} generator}
        the default is angle=pi
        .. math::
           U(\\pi) = H
        If angle and power are given both, tequila will combine them

    Returns
    -------
    QCircuit object

    """
    coef = 1 / np.sqrt(2)
    generator = lambda q: coef * (paulis.Z(q) + paulis.X(q)) - paulis.I(q)
    return _initialize_power_gate(name="H",
                                  power=power,
                                  angle=angle,
                                  target=target,
                                  control=control,
                                  generator=generator,
                                  *args,
                                  **kwargs)
コード例 #27
0
def SWAP(first: int,
         second: int,
         control: typing.Union[int, list] = None,
         power: float = None,
         *args,
         **kwargs) -> QCircuit:
    """
    Notes
    ----------
    SWAP gate, order of targets does not matter

    Parameters
    ----------
    first: int
        target qubit
    second: int
        target qubit
    control
        int or list of ints
    power
        numeric type (fixed exponent) or hashable type (parametrized exponent)

    Returns
    -------
    QCircuit

    """

    target = [first, second]
    generator = 0.5 * (paulis.X(target) + paulis.Y(target) + paulis.Z(target) -
                       paulis.I(target))
    if power is None or power in [1, 1.0]:
        return QGate(name="SWAP",
                     target=target,
                     control=control,
                     generator=generator)
    else:
        return GeneralizedRotation(angle=power * np.pi,
                                   control=control,
                                   generator=generator,
                                   eigenvalues_magnitude=0.25)
コード例 #28
0
ファイル: test_gradient.py プロジェクト: silky/tequila
def test_gradient_UY_HX_wfnsim(simulator,
                               angle_value,
                               controlled,
                               silent=True):
    # same as before just with wavefunction simulation

    # case X Y
    # U = cos(angle/2) + sin(-angle/2)*i*Y
    # <0|Ud H U |0> = cos^2(angle/2)*<0|X|0>
    # + sin^2(-angle/2) <0|YXY|0>
    # + cos(angle/2)*sin(angle/2)*i<0|XY|0>
    # + sin(-angle/2)*cos(angle/2)*(-i) <0|YX|0>
    # = cos^2*0 + sin^2*0 + cos*sin*i(<0|[XY,YX]|0>)
    # = 0.5*sin(-angle)*i <0|[XY,YX]|0> = -0.5*sin(angle)*i * 2 i <0|Z|0>
    # = sin(angle)

    angle = Variable(name="angle")
    variables = {angle: angle_value}

    qubit = 0
    H = paulis.X(qubit=qubit)
    if controlled:
        control = 1
        U = gates.X(target=control) + gates.Ry(
            target=qubit, control=control, angle=angle)
    else:
        U = gates.Ry(target=qubit, angle=angle)
    O = ExpectationValue(U=U, H=H)
    E = simulate(O, variables=variables, backend=simulator)
    dO = grad(objective=O, variable='angle')
    dE = simulate(dO, variables=variables, backend=simulator)
    E = numpy.float(E)  # for isclose
    dE = numpy.float(dE)  # for isclose
    assert (numpy.isclose(E, numpy.sin(angle(variables)), atol=0.0001))
    assert (numpy.isclose(dE, numpy.cos(angle(variables)), atol=0.0001))
    if not silent:
        print("E         =", E)
        print("sin(angle)=", numpy.sin(angle(variables)))
        print("dE        =", dE)
        print("cos(angle)=", numpy.cos(angle(variables)))
コード例 #29
0
def test_transposition():
    primitives = [paulis.X, paulis.Y, paulis.Z]
    factors = [1, -1, 1j, -1j, 0.5 + 1j]

    assert ((paulis.X(0) * paulis.X(1) * paulis.Y(2)).transpose() == -1 *
            paulis.X(0) * paulis.X(1) * paulis.Y(2))
    assert ((paulis.X(0) * paulis.X(1) *
             paulis.Z(2)).transpose() == paulis.X(0) * paulis.X(1) *
            paulis.Z(2))

    for repeat in range(10):
        string = QubitHamiltonian.unit()
        tstring = QubitHamiltonian.unit()
        for q in range(5):
            ri = random.randint(0, 2)
            P = primitives[ri]
            sign = 1
            if ri == 1:
                sign = -1
            factor = factors[random.randint(0, len(factors) - 1)]
            string *= factor * P(qubit=q)
            tstring *= factor * sign * P(qubit=q)

        assert (string.transpose() == tstring)
コード例 #30
0
ファイル: gates.py プロジェクト: sunchong137/tequila
def X(target: typing.Union[list, int],
      control: typing.Union[list, int] = None,
      power=None,
      angle=None) -> QCircuit:
    """
    Notes
    ----------
    Pauli X Gate

    Parameters
    ----------
    target
        int or list of int
    control
        int or list of int
    power
        numeric type (fixed exponent) or hashable type (parametrized exponent)
    angle
        similar to power, but will be interpreted as
        .. math::
           U(\\text{angle})=e^{-i\\frac{angle}{2} (1-X)}
        the default is angle=pi
        .. math::
           U(\\pi) = X
        If angle and power are given both, tequila will combine them

    Returns
    -------
    QCircuit object
    """

    generator = lambda q: paulis.X(q) - paulis.I(q)
    return _initialize_power_gate(name="X",
                                  power=power,
                                  angle=angle,
                                  target=target,
                                  control=control,
                                  generator=generator)