예제 #1
0
def test_endianness_simulators():
    tests = ["000111",
             "111000",
             "101010",
             "010101",
             "10010010001",
             "111100101000010"]

    for string in tests:
        binary = BitString.from_binary(binary=string)
        c = QCircuit()
        for i, v in enumerate(binary):
            if v == 1:
                c += gates.X(target=i)
            if v == 0:
                c += gates.Z(target=i)

        wfn_cirq = simulate(c, initial_state=0, backend="cirq")
        counts_cirq = simulate(c, samples=1, backend="cirq")
        counts_qiskit = simulate(c, samples=1, backend="qiskit")
        print("counts_cirq  =", type(counts_cirq))
        print("counts_qiskit=", type(counts_qiskit))
        print("counts_cirq  =", counts_cirq)
        print("counts_qiskit=", counts_qiskit)
        assert (counts_cirq.isclose(counts_qiskit))
        assert (wfn_cirq.state == counts_cirq.state)
예제 #2
0
def test_gradient_Z(simulator, power, controlled):
    qubit = 0
    control = 1
    angle = Variable(name="angle")
    if controlled:
        U = gates.X(target=control) + gates.H(target=qubit) + gates.Z(target=qubit, power=angle,
                                                                      control=control) + gates.H(target=qubit)
    else:
        U = gates.H(target=qubit) + gates.Z(target=qubit, power=angle) + gates.H(target=qubit)
    angle = Variable(name="angle")
    variables = {angle: power}
    H = paulis.Y(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))
예제 #3
0
def test_compile_ch(target, control, power):

    circuit = gates.H(target=target, control=control, power=power)
    equivalent_circuit = compile_ch(circuit)

    equivalent_ch = gates.Ry(target=target, control=None, angle=-numpy.pi / 4) + \
                    gates.Z(target=target, control=control, power=power) + \
                    gates.Ry(target=target, control=None, angle=numpy.pi / 4)

    if control is not None:
        assert (equivalent_circuit == equivalent_ch)
예제 #4
0
def test_gradient_deep_controlled_Z(simulator, power, controls):
    if controls > 2 and simulator == "qiskit":
        # does not work yet
        return
    qubit = 0
    control = [i for i in range(1, controls + 1)]
    angle = Variable(name="angle")
    U = gates.X(target=control) + gates.H(target=qubit) + gates.Z(target=qubit, power=angle, control=control) + gates.H(
        target=qubit)
    angle = Variable(name="angle")
    variables = {angle: power}
    H = paulis.Y(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))