def test_paulistring_sampling(backend, case): print(case) H = QubitHamiltonian.from_paulistrings(PauliString.from_string(case[0])) U = gates.X(target=1) + gates.X(target=3) + gates.X(target=5) E = ExpectationValue(H=H, U=U) result = simulate(E,backend=backend, samples=1) assert isclose(result, case[1], 1.e-4)
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)
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()))
def test_repetition_works(simulator, p): qubit = 0 H = paulis.Qm(qubit) U = gates.X(target=qubit) + gates.X(target=qubit) O = ExpectationValue(U=U, H=H) NM = BitFlip(p, 1) E = simulate(O, backend=simulator, samples=1, noise=NM)
def test_compile_swap(): circuit = gates.SWAP(first=0, second=3) equivalent_circuit = compile_swap(circuit) equivalent_swap = gates.X(target=0, control=3) + gates.X( target=3, control=0) + gates.X(target=0, control=3) assert (equivalent_circuit == equivalent_swap)
def test_double_cnot_bit_flip(simulator, p): qubit = 1 U = gates.X(0) + gates.X(2) + gates.CX(0, 1) + gates.CX(2, 1) H = paulis.Qm(qubit) O = ExpectationValue(U=U, H=H) NM = BitFlip(p, 2) E = simulate(O, backend=simulator, samples=1000, noise=NM) assert (numpy.isclose(E, 2 * (p - p * p), atol=1.e-1))
def test_double_cnot_bit_flip(simulator, p): qubit = 1 U = gates.X(0) + gates.X(2) + gates.CX(0, 1) + gates.CX(2, 1) H = paulis.Qm(qubit) O = ExpectationValue(U=U, H=H) NM = BitFlip(p, 2) E = simulate(O, backend=simulator, samples=1, noise=NM)
def test_controls(target, control, gate): c0 = gates.X(target=control) + gate(target=target, control=None) c1 = gates.X(target=control) + gate(target=target, control=control) wfn0 = simulate(c0, initial_state=0, backend="symbolic") wfn1 = simulate(c1, initial_state=0, backend="symbolic") assert (wfn0.isclose(wfn1)) c0 = gates.QCircuit() c1 = gate(target=target, control=control) wfn0 = simulate(c0, initial_state=0) wfn1 = simulate(c1, initial_state=0) assert (wfn0.isclose(wfn1))
def test_depolarizing_error(simulator, p, controlled): cq = 1 qubit = 0 H = paulis.Z(0) if controlled: U = gates.X(target=cq) + gates.X(target=qubit, control=cq) NM = DepolarizingError(p, 2) else: U = gates.X(target=qubit) NM = DepolarizingError(p, 1) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1, noise=NM)
def test_bit_flip(simulator, p, controlled): qubit = 0 if controlled: U = gates.X(target=1) + gates.CX(1, 0) H = paulis.Qm(0) NM = BitFlip(p, 2) else: U = gates.X(target=0) NM = BitFlip(p, 1) H = paulis.Qm(qubit) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1, noise=NM)
def test_bit_flip(simulator, p, controlled): qubit = 0 if controlled: U = gates.X(target=1) + gates.CX(1, 0) H = paulis.Qm(0) NM = BitFlip(p, 2) else: U = gates.X(target=0) NM = BitFlip(p, 1) H = paulis.Qm(qubit) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1000, noise=NM) assert (numpy.isclose(E, 1.0 - p, atol=1.e-1))
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))
def test_gradient_UX_HY_wfnsim(simulator, angle, controlled, silent=True): # same as before just with wavefunction simulation # case YX # U = cos(angle/2) + sin(-angle/2)*i*X # O = cos*sin*i*<0|YX|0> + sin*cos*(-i)<0|XY|0> # = 0.5*sin(-angle)*i <0|[YX,XY]|0> # = -sin(angle) angle_value = angle angle = Variable(name="angle") variables = {angle: angle_value} qubit = 0 H = paulis.Y(qubit=qubit) if controlled: control = 1 U = gates.X(target=control) + gates.Rx( target=qubit, control=control, angle=angle) else: U = gates.Rx(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) 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)))
def test_gradient_PHASE_HY(simulator, angle_value, controlled, silent=False): angle = Variable(name="angle") variables = {angle: angle_value} qubit = 0 H = paulis.Y(qubit=qubit) if controlled: control = 1 U = gates.X(target=control) + gates.H(target=qubit) + gates.Phase( target=qubit, control=control, phi=angle) + gates.H(target=qubit) else: U = gates.H(target=qubit) + gates.Phase( target=qubit, phi=angle) + gates.H(target=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) 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(variables))) print("dE =", dE) print("-cos(angle)=", -numpy.cos(angle(variables)))
def test_phase_amp_damp(simulator, p): qubit = 0 H = paulis.Z(0) U = gates.X(target=qubit) O = ExpectationValue(U=U, H=H) NM = PhaseAmplitudeDamp(p, 1 - p, 1) E = simulate(O, backend=simulator, samples=1, noise=NM)
def test_amp_damp(simulator, p): qubit = 0 H = (0.5) * (paulis.I(0) - paulis.Z(0)) U = gates.X(target=qubit) O = ExpectationValue(U=U, H=H) NM = AmplitudeDamp(p, 1) E = simulate(O, backend=simulator, samples=1, noise=NM)
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)
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)
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))
def test_endianness_simulators(): tests = [ "000111", "111000", "101010", "010101", "10010010001", "111100101000010" ] for string in tests: number = int(string, 2) binary = BitString.from_binary(binary=string) c = QCircuit() for i, v in enumerate(binary): if v == 1: c += gates.X(target=i) c += gates.Measurement(target=[x for x in range(len(string))]) 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 == counts_qiskit) assert (wfn_cirq.state == counts_cirq.state)
def test_rz_phase_flip_1(simulator, p, angle): U = gates.X(target=0) + gates.H(1) + gates.CRz(control=0, target=1, angle=Variable('a')) + gates.H(1) H = paulis.Z(1) * paulis.I(0) O = ExpectationValue(U, H) NM = PhaseFlip(p, 2) E = simulate(O, backend=simulator, variables={'a': angle}, samples=1000, noise=NM) print(E) assert (numpy.isclose(E, ((1.0 - 2 * p) ** 2) * numpy.cos(angle), atol=1.e-1))
def test_qubit_excitations(): H = paulis.Projector("1.0*|100>") U1 = gates.X(0) + gates.QubitExcitation( target=[0, 1], angle="a", assume_real=True) U2 = gates.X(0) + gates.Trotterized( generators=[U1.gates[1].make_generator()], angles=["a"], steps=1) E1 = ExpectationValue(H=H, U=U1) E2 = ExpectationValue(H=H, U=U2) dE1 = grad(E1, "a") dE2 = grad(E2, "a") for a in numpy.random.uniform(-numpy.pi, numpy.pi, 5): a = float(a) variables = {"a": a} wfn1 = simulate(U1, variables=variables) wfn2 = simulate(U2, variables=variables) F = numpy.abs(wfn1.inner(wfn2))**2 assert numpy.isclose(F, 1.0, 1.e-4) eval1 = simulate(dE1, variables=variables) eval2 = simulate(dE2, variables=variables) assert numpy.isclose(eval1, eval2, 1.e-4) H = paulis.Projector("1.0*|0110>") U1 = gates.X([1, 2]) + gates.QubitExcitation( target=[0, 1, 3, 2], angle="a", assume_real=True) U2 = gates.X([1, 2]) + gates.Trotterized( generators=[U1.gates[2].make_generator()], angles=["a"], steps=1) E1 = ExpectationValue(H=H, U=U1) E2 = ExpectationValue(H=H, U=U2) dE1 = grad(E1, "a") dE2 = grad(E2, "a") for a in numpy.random.uniform(-numpy.pi, numpy.pi, 5): a = float(a) variables = {"a": a} wfn1 = simulate(U1, variables=variables) wfn2 = simulate(U2, variables=variables) F = numpy.abs(wfn1.inner(wfn2))**2 assert numpy.isclose(F, 1.0, 1.e-4) eval1 = simulate(dE1, variables=variables) eval2 = simulate(dE2, variables=variables) assert numpy.isclose(eval1, eval2, 1.e-4)
def test_rx_bit_flip_1(simulator, p, angle): U = gates.X(target=0) + gates.CRx(control=0, target=1, angle="a") H = paulis.Z(1) * paulis.I(0) NM = BitFlip(p, 2) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1000, variables={'a': angle}, noise=NM) print(E) print(p + numpy.cos(angle) - p * numpy.cos(angle)) assert (numpy.isclose(E, p + numpy.cos(angle) - p * numpy.cos(angle), atol=1.e-1))
def test_phase_amp_damp_is_both(simulator, p): qubit = 0 H = paulis.Z(0) U = gates.X(target=qubit) O = ExpectationValue(U=U, H=H) NM1 = PhaseDamp(1 - p, 1) + AmplitudeDamp(p, 1) E1 = simulate(O, backend=simulator, samples=1, noise_model=NM1) NM2 = PhaseAmplitudeDamp(p, 1 - p, 1) E2 = simulate(O, backend=simulator, samples=1, noise_model=NM2)
def test_compile_y(target, control, power): circuit = gates.Y(target=target, control=control, power=power) equivalent_circuit_y = compile_y(circuit) equivalent_y = gates.Rz(target=target, control=None, angle=-numpy.pi / 2) + \ gates.X(target=target, control=control, power=power) + \ gates.Rz(target=target, control=None, angle=numpy.pi / 2) assert (equivalent_circuit_y == equivalent_y)
def test_controlled_rotations(simulator): angles = uniform(0, 2 * pi, 5) gs = [gates.Rx, gates.Ry, gates.Rz] for angle in angles: for gate in gs: qubit = randint(0, 1) control = randint(2, 3) U = gates.X(target=control) + gate(target=qubit, control=control, angle=angle) RCU = compile_controlled_rotation(gate=U) wfn1 = simulate(U, initial_state=0, backend=simulator) wfn2 = simulate(RCU, initial_state=0, backend=simulator) assert (isclose(numpy.abs(wfn1.inner(wfn2)) ** 2, 1.0, atol=1.e-4))
def test_rz_phase_flip_1(simulator, p, angle): U = gates.X(target=0) + gates.H(1) + gates.CRz( control=0, target=1, angle=Variable('a')) + gates.H(1) H = paulis.Z(1) * paulis.I(0) O = ExpectationValue(U, H) NM = PhaseFlip(p, 2) E = simulate(O, backend=simulator, variables={'a': angle}, samples=1, noise=NM) print(E)
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)
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)
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)