from qiskit.opflow import I, X, Y, Z
print(I, X, Y, Z)

print(1.5 * I)
print(2.5 * X)

print(X + 2.0 * Y)

print(X^Y^Z)

print(X @ Y @ Z)

print((X + Y) @ (Y + Z))

print((X + Y) ^ (Y + Z))

(I, X)

2.0 * X^Y^Z

print(1.1 * ((1.2 * X)^(Y + (1.3 * Z))))

from qiskit.opflow import (StateFn, Zero, One, Plus, Minus, H,
                           DictStateFn, VectorStateFn, CircuitStateFn, OperatorStateFn)

print(Zero, One)

print(Plus, Minus)

print(Zero.eval('0'))
print(Zero.eval('1'))
print(One.eval('1'))
print(Plus.eval('0'))
print(Minus.eval('1'))

One.adjoint()

~One

(2.0 + 3.0j) * Zero

print(Zero + One)

import math

v_zero_one = (Zero + One) / math.sqrt(2)
print(v_zero_one)

print(Plus + Minus)

print(~One @ One)

(~One @ One).eval()

(~v_zero_one @ v_zero_one).eval()

(~Minus @ One).eval()

print((~One).compose(One))

(~One).eval(One)

print(Zero^Plus)

print((Zero^Plus).to_circuit_op())

print(600 * ((One^5) + (Zero^5)))
print((One^Zero)^3)

print(((Plus^Minus)^2).to_matrix_op())
print(((Plus^One)^2).to_circuit_op())
print(((Plus^One)^2).to_matrix_op().sample())

print(StateFn({'0':1}))
print(StateFn({'0':1}) == Zero)

print(StateFn([0,1,1,0]))

from qiskit.circuit.library import RealAmplitudes
print(StateFn(RealAmplitudes(2)))

from qiskit.opflow import X, Y, Z, I, CX, T, H, S, PrimitiveOp

X

print(X.eval('0'))

X.eval('0').eval('1')

print(CX)
print(CX.to_matrix().real) # The imaginary part vanishes.

CX.eval('01')  # 01 is the one in decimal. We get the first column.

CX.eval('01').eval('11')  # This returns element with (zero-based) index (1, 3)

print(X @ One)

(X @ One).eval()

X.eval(One)

print(((~One^2) @ (CX.eval('01'))).eval())

print(((H^5) @ ((CX^2)^I) @ (I^(CX^2)))**2)
print((((H^5) @ ((CX^2)^I) @ (I^(CX^2)))**2) @ (Minus^5))
print(((H^I^I)@(X^I^I)@Zero))

print(~One @ Minus)

from qiskit.opflow import ListOp

print((~ListOp([One, Zero]) @ ListOp([One, Zero])))

print((~ListOp([One, Zero]) @ ListOp([One, Zero])).reduce())

print(StateFn(Z).adjoint())
StateFn(Z).adjoint()

print(StateFn(Z).adjoint().eval(Zero))
print(StateFn(Z).adjoint().eval(One))
print(StateFn(Z).adjoint().eval(Plus))

import numpy as np
from qiskit.opflow import I, X, Y, Z, H, CX, Zero, ListOp, PauliExpectation, PauliTrotterEvolution, CircuitSampler, MatrixEvolution, Suzuki
from qiskit.circuit import Parameter
from qiskit import Aer

two_qubit_H2 =  (-1.0523732 * I^I) + \
                (0.39793742 * I^Z) + \
                (-0.3979374 * Z^I) + \
                (-0.0112801 * Z^Z) + \
                (0.18093119 * X^X)

print(two_qubit_H2)

evo_time = Parameter('θ')
evolution_op = (evo_time*two_qubit_H2).exp_i()
print(evolution_op) # Note, EvolvedOps print as exponentiations
print(repr(evolution_op))

h2_measurement = StateFn(two_qubit_H2).adjoint()
print(h2_measurement)

bell = CX @ (I ^ H) @ Zero
print(bell)

evo_and_meas = h2_measurement @ evolution_op @ bell
print(evo_and_meas)

trotterized_op = PauliTrotterEvolution(trotter_mode=Suzuki(order=2, reps=1)).convert(evo_and_meas)
# We can also set trotter_mode='suzuki' or leave it empty to default to first order Trotterization.
print(trotterized_op)

bound = trotterized_op.bind_parameters({evo_time: .5})

bound[1].to_circuit().draw()

# Note that XX was the only non-diagonal measurement in our H2 Observable
print(PauliExpectation(group_paulis=False).convert(h2_measurement))

print(PauliExpectation().convert(h2_measurement))

diagonalized_meas_op = PauliExpectation().convert(trotterized_op)
print(diagonalized_meas_op)

evo_time_points = list(range(8))
h2_trotter_expectations = diagonalized_meas_op.bind_parameters({evo_time: evo_time_points})

h2_trotter_expectations.eval()

sampler = CircuitSampler(backend=Aer.get_backend('aer_simulator'))
# sampler.quantum_instance.run_config.shots = 1000
sampled_trotter_exp_op = sampler.convert(h2_trotter_expectations)
sampled_trotter_energies = sampled_trotter_exp_op.eval()
print('Sampled Trotterized energies:\n {}'.format(np.real(sampled_trotter_energies)))

print('Before:\n')
print(h2_trotter_expectations.reduce()[0][0])
print('\nAfter:\n')
print(sampled_trotter_exp_op[0][0])

import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright