Skip to content

Commit

Permalink
Merge pull request #78 from UCL-CCS/haar_random
Browse files Browse the repository at this point in the history
haar random quantum state and Pauliword op functionality added
  • Loading branch information
TimWeaving authored Oct 4, 2022
2 parents 87f0f5f + 9505590 commit 14820cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions notebooks/Chemistry usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6276,7 +6276,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -6290,7 +6290,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.7"
},
"vscode": {
"interpreter": {
Expand Down
42 changes: 42 additions & 0 deletions symmer/symplectic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from openfermion import QubitOperator
from qiskit.quantum_info import Pauli
from qiskit.opflow import PauliOp, PauliSumOp
from scipy.stats import unitary_group
import warnings
warnings.simplefilter('always', UserWarning)

Expand Down Expand Up @@ -63,6 +64,22 @@ def random(cls,
coeff_vec += 1j * np.random.randn(n_terms)
return cls(symp_matrix, coeff_vec)

@classmethod
def haar_random(cls,
n_qubits: int,
) -> "PauliwordOp":
""" Generate a Haar random U(N) matrix (N^n_qubits) as a linear combination of Pauli operators.
aka generate a uniform random unitary from a Hilbert space.
Args:
n_qubits: number of qubits
Returns:
p_random (PauliwordOp): Haar random matrix in Pauli basis
"""
haar_matrix = unitary_group.rvs(2**n_qubits)
p_random = cls.from_matrix(haar_matrix)
return p_random

@classmethod
def from_list(cls,
pauli_terms :List[str],
Expand Down Expand Up @@ -846,6 +863,31 @@ def copy(self) -> "QuantumState":
"""
return deepcopy(self)

@classmethod
def haar_random(cls,
n_qubits: int,
vec_type: str) -> "QuantumState":
"""
Generate a Haar random quantum state - (uniform random quantum state).
Args:
n_qubits: number of qubits
vec_type (str): bra or ket
Returns:
qstate_random (QuantumState): Haar random quantum state
"""
if vec_type=='ket':
haar_vec = (unitary_group.rvs(2**n_qubits)[:,0]).reshape([-1, 1])
elif vec_type == 'bra':
haar_vec = (unitary_group.rvs(2**n_qubits)[0,:]).reshape([1, -1])
else:
raise ValueError(f'vector type: {vec_type} unkown')

qstate_random = cls.from_array(haar_vec)
return qstate_random

def __str__(self) -> str:
"""
Defines the print behaviour of QuantumState - differs depending on vec_type
Expand Down

0 comments on commit 14820cc

Please sign in to comment.