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))
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)
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)
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
def test_qubit_wise_commuting(): ''' Testing whether method is_qubit_wise_commuting correctly recognizes qubit wise commuting parts. ''' not_qwc = -1.0 * paulis.Z(0) * paulis.Z(1) - 0.5 * paulis.Y(0) * paulis.Y(1) not_qwc = BinaryHamiltonian.init_from_qubit_hamiltonian(not_qwc) qwc = paulis.Z(0) * paulis.Z(1) + paulis.Z(1) * paulis.Y(2) qwc = BinaryHamiltonian.init_from_qubit_hamiltonian(qwc) assert not not_qwc.is_qubit_wise_commuting() assert qwc.is_qubit_wise_commuting()
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)
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
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_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))
def __init__(self, phase, target: list, control: list = None): assert (phase is not None) super().__init__(eigenvalues_magnitude=0.5, name='Phase', parameter=phase, target=target, control=control) self.generator = paulis.Z(target) - paulis.I(target)
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_rx_bit_flip_0(simulator, p, angle): U = gates.Rx(target=0, angle=Variable('a')) H = paulis.Z(0) NM = BitFlip(p, 1) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1000, variables={'a': angle}, noise=NM) assert (numpy.isclose(E, (1 - 2 * 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_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_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
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_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_rx_bit_flip_0(simulator, p, angle): U = gates.Rx(target=0, angle=Variable('a')) H = paulis.Z(0) NM = BitFlip(p, 1) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1, variables={'a': angle}, noise=NM)
def test_convenience(): i = numpy.random.randint(0, 10, 1)[0] assert paulis.X(i) + paulis.I(i) == paulis.X(i) + 1.0 assert paulis.Qp(i) == 0.5 * (1.0 + paulis.Z(i)) assert paulis.Qm(i) == 0.5 * (1.0 - paulis.Z(i)) assert paulis.Sp(i) == 0.5 * (paulis.X(i) + 1.j * paulis.Y(i)) assert paulis.Sm(i) == 0.5 * (paulis.X(i) - 1.j * paulis.Y(i)) i = numpy.random.randint(0, 10, 1)[0] assert paulis.Qp(i) == (0.5 + 0.5 * paulis.Z(i)) assert paulis.Qm(i) == (0.5 - 0.5 * paulis.Z(i)) assert paulis.Sp(i) == (0.5 * paulis.X(i) + 0.5j * paulis.Y(i)) assert paulis.Sm(i) == (0.5 * paulis.X(i) - 0.5j * paulis.Y(i)) assert -1.0 * paulis.Y(i) == -paulis.Y(i) test = paulis.Z(i) test *= -1.0 assert test == -paulis.Z(i) test = paulis.Z(i) test += 1.0 assert test == paulis.Z(i) + 1.0 test = paulis.X(i) test += paulis.Y(i + 1) assert test == paulis.X(i) + paulis.Y(i + 1) test = paulis.X(i) test -= paulis.Y(i) test += 3.0 test = -test assert test == -1.0 * (paulis.X(i) - paulis.Y(i) + 3.0) test = paulis.X([0, 1, 2, 3]) assert test == QubitHamiltonian.from_string("X(0)X(1)X(2)X(3)", False) test = paulis.Y([0, 1, 2, 3]) assert test == QubitHamiltonian.from_string("Y(0)Y(1)Y(2)Y(3)", False) test = paulis.Z([0, 1, 2, 3]) assert test == QubitHamiltonian.from_string("Z(0)Z(1)Z(2)Z(3)", False)
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 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
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)
def test_rx_bit_flip_1(simulator, p, angle): qubit = 1 U = gates.X(target=0) + gates.CRx(control=0, target=1, angle=Variable('a')) H = paulis.Z(1) * paulis.I(0) NM = BitFlip(p, 2) O = ExpectationValue(U=U, H=H) E = simulate(O, backend=simulator, samples=1, variables={'a': angle}, noise=NM) print(E) print(p + numpy.cos(angle) - p * numpy.cos(angle))
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)
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)
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)
def Z(target: typing.Union[list, int], control: typing.Union[list, int] = None, power=None, angle=None) -> QCircuit: """ Notes ---------- Pauli Z 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-Z)} the default is angle=pi .. math:: U(\\pi) = Z If angle and power are given both, tequila will combine them Returns ------- QCircuit object """ generator = lambda q: paulis.Z(q) - paulis.I(q) return _initialize_power_gate(name="Z", power=power, angle=angle, target=target, control=control, generator=generator)