-
Notifications
You must be signed in to change notification settings - Fork 20
added tensor_up feature #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,11 @@ | |
| Note: uses SciPy sparse diagonal (DIA) representation to increase space and | ||
| timeefficiency. | ||
| """ | ||
| import warnings | ||
| import scipy.sparse as sps | ||
| from referenceqvm.gates import gate_matrix | ||
| from pyquil.quilbase import * | ||
| from pyquil.paulis import PauliSum | ||
|
|
||
| """ | ||
| If True, only physically-implementable operations allowed! | ||
|
|
@@ -355,6 +357,51 @@ def tensor_gates(gate_set, defgate_set, pyquil_gate, num_qubits): | |
| return gate | ||
|
|
||
|
|
||
| def tensor_up(pauli_terms, num_qubits): | ||
| """ | ||
| Takes a PauliSum object along with a total number of | ||
| qubits and returns a matrix corresponding the tensor representation of the | ||
| object. | ||
|
|
||
| Useful for generating the full Hamiltonian after a particular fermion to | ||
| pauli transformation. For example: | ||
|
|
||
| Converting a PauliSum X0Y1 + Y1X0 into the matrix | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| [[ 0.+0.j, 0.+0.j, 0.+0.j, 0.-2.j], | ||
| [ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], | ||
| [ 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], | ||
| [ 0.+2.j, 0.+0.j, 0.+0.j, 0.+0.j]] | ||
|
|
||
|
|
||
| :param pauli_terms: (PauliSum) object of PauliTerm | ||
| :param num_qubits: (int) number of qubits in the system | ||
| :returns: (numpy array) representation of the paui_terms operator | ||
| """ | ||
| if not isinstance(pauli_terms, PauliSum): | ||
| raise TypeError("can only tensor PauliSum") | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might wanna check for sanity if the operator gets too big. If you kron up 100 terms then the final matrix will be gargantuan and might kill you.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do that. I believe in the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see that argument. Maybe we can find a middle ground and generate a warning (https://docs.python.org/3/library/warnings.html#module-warnings) beyond some semi-reasonable limit. This will not interrupt the code execution but warn the user that code with large number of qubits might have a performance issue |
||
| # check if operator is valid w.r.t the input number of qubits | ||
| for term in pauli_terms.terms: | ||
| if len(term._ops.keys()) > 0: | ||
| if max(term._ops.keys()) >= num_qubits: | ||
| raise IndexError("pauli_terms has higher index than qubits") | ||
|
|
||
| big_hilbert = np.zeros((2 ** num_qubits, 2 ** num_qubits), dtype=complex) | ||
| # left kronecker product corresponds to the correct basis ordering | ||
| for term in pauli_terms.terms: | ||
|
|
||
| tmp_big_hilbert = np.array([1]) | ||
| for index in range(num_qubits): | ||
| tmp_big_hilbert = np.kron(gate_matrix[term[index]], tmp_big_hilbert) | ||
|
|
||
| big_hilbert += tmp_big_hilbert * term.coefficient | ||
|
|
||
| return big_hilbert | ||
|
|
||
|
|
||
| def value_get(param_obj): | ||
| """ | ||
| Function that returns the raw number / string stored in certain pyQuil | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't clarify the use case. If you say that then you should give an example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup.