Skip to content

Commit ab5b92d

Browse files
dongreenbergmanoelmarqueswoodsp-ibm
authored
* Add abelian property to opvecs and AbelianGrouper to operator init. * Break up PauliCoB big clifford synthesis function into smaller ones. * Add AbelianGrouper test. * Add better input checking in primitives and remove unnecessary print. * Fix coeffs bugs in pauli_cob.py. * Reorganize pauli_cob. All tests pass, with grouping on and off. * Change expectation_value backends to work through setters. * Reorganize local_simulator_sampler.py a bit to use it in a test. * Grouping Paulis works!! All tests pass. * Add "compute TPB pauli" function to pauli_cob. * Add WIP attempt at evolution over Abelian paulis. * Fix trotter bug. * Fix some other Trotter bugs. * Add parameters to OpPaulis and test. Parameterized evolution passes!!! * Add parameter binding for Op coefficients. * Add parameter binding, and binding tests. Tests pass. * Add division to Operators to make normalization convenient. * Finish merging MinEigenSolver PR. All tests pass. * Update QAOA, all tests pass!! * Update some QAOA imports and typehints. * Add QDrift trotterization method. All tests pass. * Start migrating QPE, tests fail. * fix spell * fix almost all style errors * fix copyright * fix import cycles, changed to relative imports when possible * relative imports * Add bind_params to state_fn_circuit.py and op_circuit.py, and tests. Add list unrolling for param binding, and tests. Tests pass. * Add param list handling for all Op types. * Make OpVec printing nicer. * Add op_converter to imports for better backwards compatibility. * Add AerPauliExpectation tests. Tests pass. Issue with Aer though. * Fix a few AerPauliExpectation bugs * Start building toward parameterized Qobj. * fix some lint errors * fix some lint errors * fix style * fix copyright * set style to defaults, fix aqua unit test loading * change loading tests qpe/iqpe * Fix OpPrimitive lint errors. * Fix operator_base.py lint errors. * Fix state_fn.py lint errors. * Fix OpVec lint errors. * Fix state_fn_circuit.py lint errors. * Fix state_fn_dict.py lint errors. * Fix state_fn_operator.py lint errors. * Fix state_fn_vector.py lint errors. Tests pass. * Fix QDrift test to deal with first Op in trotterization list being OpCircuit. * Fix op_circuit.py lint errors. * Fix op_pauli.py lint errors. * Fix op_composition.py lint errors. * Fix op_kron.py lint errors. * Fix abelian_grouper.py lint errors. Tests pass. * Fix pauli_cob.py lint errors. Tests pass. * Fix Expectation lint errors. Tests pass. * Fix circuit sampler lint errors. Tests pass. * Fix other expectation lint errors. Tests pass. * Fix trotterization lint errors. Tests pass. * Add MatrixEvolution shell, fix evolution lint errors. * Fix bug in evolution tests after fixing lint errors. * Fix cyclic import for lint. * fix pylint cyclic import error * Make tests pass. Add aux_ops back to VQE, and make VQE and QAOA take old or new ops. * fix spell and lint * Fix swapping issue in evolution. * Fix composition in OpEvolution. * Fix add OpSum and kron OpKron in OpEvolution. * Add to_opflow to legacy base_operator * Clean OpCircuit and StateFnCircuit __str__ * Fix qaoa mixer. * fix spell,style * Ok now really all tests pass. * add to_matrix_op() methods to ops. * Start migrating NumpyEigensolver for Opflow * Start removing back from op_primitive eval functions. All tests pass. * Update eval logic (to be able to remove back) for operator_combos. * Add to_matrix_op for OpMatrix and StateFnVector, and change some `if back`s to `if back is not None` * Finish decoupling back args from evals. All tests pass. * Remove back from eval logic. * Remove back from eval. All tests pass. * Change matrix_expectation.py to rely on to_matrix_op. * Migrate numpy_eigen_solver.py and numpy_minimum_eigen_solver. * Remove ToMatrixOp converter. * set VQE _auto_conversion to False for now * Add sampling and tests. Fix a rounding error in a test. Fix a not none error in numpy_eigen_solver.py. * Add array methods to OpVec. Fix typo in OpPauli. Allow reverse_endianness in to_opflow for WeightedPauli. * Make NumpyEigensolver return a StateFn in result.eigenstate. * Fix flaky optimization tests. Fix OpVec so iterator interface works. * Fix StateFnVector sampling. Fix sparse NumpyEigensolution. Fix some aux_op stuff. Fix some other things here and there. Please no more breakage. * Change some sparsity stuff. * fix spelling * Typehints. * More typehints * fix copyright * fix spelling * More typehints, make globals immutable. * fix style * Rearrange tests, Add CZ to globals. * Refactor some names. * Rename OpEvolution to EvolutionOp. Tests pass. * Rename primitive ops. All tests pass. * Finish renamings. * Test IBMQ Pauli expectation. All tests pass. * Update spelling. * Update Pauli to num_qubits. * Updating some naming. * Add diag support to fix knapsack issue. * fix unit test * fix unit test * fix travis * Turn half of Steve's comments. * Fix some exponentiation things. * Fix some exponentiation things. * Add trotterization_factory. All tests pass. * Add evolution_factory. All tests pass. * Add circuit_sampler_factory. All tests pass. * Rename get_primitives to primitive_strings. Turn some of Julien's changes. * Only allow sample_circuits to accept circuit_ops. Tests pass. * Turn more review changes * fix spell, style * Add matrix_op exp_i() into HamiltonianGate. Tests fail due to CircuitOp decompose() during composition. If commented out (line 158) tests pass. * Change CircuitOp and StateFnCircuit to rely on QuantumCircuit instead of Instruction. All tests pass. Add to_circuit_op to relevant ops. Solves all the decompose issues. * Add matrix_evolution and update QAOA to test matrix_op for cost operator. Add logic to update UCCSD to operator flow. Tests pass. * Delete PauliToInstruction, as it's obsolete. * Add to_pauli_op and tests. Tests pass. * Fix composed_op.py eval bug * Add sig digit rounding. VQE tests fail. * better precision for sig digit rounding. Tests pass. * Fix pep8, add comment * Add to_circuit_op to statefns, making DictToCircuit mostly obsolete. Tests pass. * fix cyclic imports * fix numpy boolean to string coercion * Update repr and a docstring. * Make ExpectationValues into converters. Test pass. * Fix bug from merge. * Fix bugs, make Minus just a CircuitStateFn and not a ComposedOp. * Uncomment HamiltonianGate * Update lots of docstrings part I. Tests pass. * fix docstring * More docstrings. Change class.rst so docs are generated for some python operator overloads. * Add navigation structure for docs to init files * More docs. * fix doctrings * 1) Change local_simulator_sampler.py to circuit_sampler.py 2) Set up circuit_samplers directory to be removed. 3) Add IBMQ VQE test. 4) Change AerPauliExpectation and CircuitSampler to handle expval_measurement/snapshots correctly. Tests pass. * 1) Delete circuit_samplers. 2) Allow CircuitSampler to attach_results. * Update Operator init * Change Operator directory names. Tests pass. * fix spell, docs * Turn Expectations purely into converters. Tests pass. * fix docs * skip IBMQ test * Add Converters docs. Tests pass. * fix spell * Add Evolutions docs. Tests pass. * Add Expectation docs. Tests pass. * fix spell * Add StateFn docs. Tests pass. * Fix typo. * Add ListOp init docs. * Fix some ordering * Little docs edits. * fix spell * Little docs edits. * 1) Add to_legacy_op to OperatorBase to allow non-migrated algos to accept new Operators. 2) Allow QPE and iQPE to accept new Operators, migrate tests. Tests pass. * Fix typehints for minimum_eigen_solvers * Make sure expectations can handle mixed observables. * fix spell * Turn some more of Steve's comments. Tests pass. * Turn some more of Steve's comments. Fix a buncha parameter stuff, and make sure mixed Pauli evolution works. * Turn some more of Steve's comments. Tests pass. * Turn some comments, fix a QAOA bug. * Try collapsing ListOp to_matrix a bit. * Turn more comments, fix some bugs. * Turn more comments, fix some bugs. * Update ListOp docs. * Update ListOp docs. * Update ListOp docs. * fix docstring * Update minimum_eigen_solvers setter typehints. * Add Changelog and tests for DictToCircuitSum. * Update VQE's construct_circuit and some changelog elements. * fix spell * Allow MinEigenOptimizer to accept StateFn result in result.eigenstate. * fix style * Update changelog with more detail. Update VQE to call super. * Typo Co-authored-by: Manoel Marques <manoel@us.ibm.com> Co-authored-by: woodsp <woodsp@us.ibm.com>
1 parent ff02971 commit ab5b92d

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

qiskit/finance/applications/ising/portfolio.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ def get_operator(mu, sigma, q, budget, penalty): # pylint: disable=invalid-name
5757
E = np.matmul(np.asmatrix(e).T, np.asmatrix(e))
5858

5959
# map problem to Ising model
60-
offset = -1*np.dot(mu, e)/2 + penalty*budget**2 - \
61-
budget*n*penalty + n**2*penalty/4 + q/4*np.dot(e, np.dot(sigma, e))
62-
mu_z = mu/2 + budget*penalty*e - n*penalty/2*e - q/2*np.dot(sigma, e)
63-
sigma_z = penalty/4*E + q/4*sigma
60+
offset = -1 * np.dot(mu, e) / 2 + penalty * budget ** 2 - \
61+
budget * n * penalty + n ** 2 * penalty / 4 + q / 4 * np.dot(e, np.dot(sigma, e))
62+
mu_z = mu / 2 + budget * penalty * e - n * penalty / 2 * e - q / 2 * np.dot(sigma, e)
63+
sigma_z = penalty / 4 * E + q / 4 * sigma
6464

6565
# construct operator
6666
pauli_list = []
6767
for i in range(n):
6868
i_ = i
69-
# i_ = n-i-1
69+
# i_ = n - i - 1
7070
if np.abs(mu_z[i_]) > 1e-6:
7171
xp = np.zeros(n, dtype=np.bool)
7272
zp = np.zeros(n, dtype=np.bool)
@@ -80,7 +80,7 @@ def get_operator(mu, sigma, q, budget, penalty): # pylint: disable=invalid-name
8080
zp = np.zeros(n, dtype=np.bool)
8181
zp[i_] = True
8282
zp[j_] = True
83-
pauli_list.append([2*sigma_z[i_, j_], Pauli(zp, xp)])
83+
pauli_list.append([2 * sigma_z[i_, j_], Pauli(zp, xp)])
8484
offset += sigma_z[i_, i_]
8585

8686
return WeightedPauliOperator(paulis=pauli_list), offset

qiskit/finance/applications/ising/portfolio_diversification.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import numpy as np
1818
from qiskit.quantum_info import Pauli
1919

20-
from qiskit.aqua.operators import WeightedPauliOperator
20+
from qiskit.aqua.operators import WeightedPauliOperator, StateFn
2121

2222

2323
def get_operator(rho, n, q):
@@ -46,52 +46,52 @@ def get_operator(rho, n, q):
4646
Q2 = np.zeros([N, N])
4747
Q3 = np.zeros([N, N])
4848

49-
for x in range(n**2, n**2+n):
49+
for x in range(n ** 2, n ** 2 + n):
5050
q0[x] = 1
5151

52-
Q0 = A*np.dot(q0, q0.T)
52+
Q0 = A * np.dot(q0, q0.T)
5353
for ii in range(0, n):
5454
v0 = np.zeros([N, 1])
55-
for jj in range(n*ii, n*(ii+1)):
55+
for jj in range(n * ii, n * (ii + 1)):
5656
v0[jj] = 1
5757
Q1 = Q1 + np.dot(v0, v0.T)
58-
Q1 = A*Q1
58+
Q1 = A * Q1
5959

6060
for jj in range(0, n):
6161
v0 = np.zeros([N, 1])
62-
v0[n*jj+jj] = 1
63-
v0[n**2+jj] = -1
62+
v0[n * jj + jj] = 1
63+
v0[n ** 2 + jj] = -1
6464
Q2 = Q2 + np.dot(v0, v0.T)
65-
Q2 = A*Q2
65+
Q2 = A * Q2
6666

6767
for ii in range(0, n):
6868
for jj in range(0, n):
69-
Q3[ii*n + jj, n**2+jj] = -0.5
69+
Q3[ii * n + jj, n ** 2 + jj] = -0.5
7070
Q3[n ** 2 + jj, ii * n + jj] = -0.5
7171

7272
Q3 = A * Q3
7373

74-
Q = Q0+Q1+Q2+Q3
74+
Q = Q0 + Q1 + Q2 + Q3
7575

7676
# linear term c:
7777
c0 = np.zeros(N)
7878
c1 = np.zeros(N)
7979
c2 = np.zeros(N)
8080
c3 = np.zeros(N)
8181

82-
for x in range(n**2):
82+
for x in range(n ** 2):
8383
c0[x] = instance_vec[x]
84-
for x in range(n**2, n**2+n):
85-
c1[x] = -2*A*q
86-
for x in range(n**2):
87-
c2[x] = -2*A
88-
for x in range(n**2):
84+
for x in range(n ** 2, n ** 2 + n):
85+
c1[x] = -2 * A * q
86+
for x in range(n ** 2):
87+
c2[x] = -2 * A
88+
for x in range(n ** 2):
8989
c3[x] = A
9090

91-
g = c0+c1+c2+c3
91+
g = c0 + c1 + c2 + c3
9292

9393
# constant term r
94-
c = A*(q**2 + n)
94+
c = A * (q ** 2 + n)
9595

9696
# Defining the new matrices in the Z-basis
9797

@@ -142,14 +142,16 @@ def get_portfoliodiversification_solution(rho, n, q, result): # pylint: disable
142142
# pylint: disable=invalid-name
143143
del rho, q # unused
144144
v = result['eigvecs'][0]
145+
if isinstance(v, StateFn):
146+
v = v.to_matrix()
145147
# N = (n + 1) * n # number of qubits
146148
N = n ** 2 + n
147149

148150
index_value = [x for x in range(len(v)) if v[x] == max(v)][0]
149151
string_value = "{0:b}".format(index_value)
150152

151153
while len(string_value) < N:
152-
string_value = '0'+string_value
154+
string_value = '0' + string_value
153155

154156
x_state = list()
155157
for elements in string_value:

test/finance/test_portfolio_diversification.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def cplex_solution(self):
6767

6868
my_rhs = [q] + [1 for x in range(0, n)] + \
6969
[0 for x in range(0, n)] + [0.1 for x in range(0, n ** 2)]
70-
my_sense = "".join(['E' for x in range(0, 1+n)]) + \
70+
my_sense = "".join(['E' for x in range(0, 1 + n)]) + \
7171
"".join(['E' for x in range(0, n)]) + \
7272
"".join(['L' for x in range(0, n ** 2)])
7373

@@ -100,12 +100,12 @@ def _populate_by_row(self, prob, my_obj, my_ub, my_lb, my_ctype, my_sense, my_rh
100100
prob.set_results_stream(None)
101101

102102
rows = []
103-
col = list(range(n**2, n**2+n))
103+
col = list(range(n ** 2, n ** 2 + n))
104104
coef = [1 for x in range(0, n)]
105105
rows.append([col, coef])
106106

107107
for i_i in range(0, n):
108-
col = list(range(0+n*i_i, n+n*i_i))
108+
col = list(range(0 + n * i_i, n + n * i_i))
109109
coef = [1 for x in range(0, n)]
110110

111111
rows.append([col, coef])
@@ -117,7 +117,7 @@ def _populate_by_row(self, prob, my_obj, my_ub, my_lb, my_ctype, my_sense, my_rh
117117

118118
for i_i in range(0, n):
119119
for j_j in range(0, n):
120-
col = [i_i*n + j_j, n ** 2 + j_j]
120+
col = [i_i * n + j_j, n ** 2 + j_j]
121121
coef = [1, -1]
122122

123123
rows.append([col, coef])

0 commit comments

Comments
 (0)