some docs
This commit is contained in:
parent
32ba69a3b2
commit
e912506375
@ -1,5 +1,6 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
# Raw matrixes to be used for initialization of qubits and gates
|
||||||
__0 = [[1],
|
__0 = [[1],
|
||||||
[0]]
|
[0]]
|
||||||
|
|
||||||
@ -40,10 +41,21 @@ _CNOT = [
|
|||||||
|
|
||||||
|
|
||||||
class State(object):
|
class State(object):
|
||||||
|
"""Represents a quantum state"""
|
||||||
def __init__(self, matrix_state):
|
def __init__(self, matrix_state):
|
||||||
|
"""
|
||||||
|
Can be initialized with a matrix, e.g. for |0> this is [[0],[1]]
|
||||||
|
:param matrix_state: a matrix representing the quantum state
|
||||||
|
"""
|
||||||
self.matrix_state = np.array(matrix_state)
|
self.matrix_state = np.array(matrix_state)
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
|
"""
|
||||||
|
Kind of hacky way to store a substate of an item for use in Gate operations
|
||||||
|
so that one can use state[0] and the encompassing operator can access this.
|
||||||
|
:param item:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
if item >= len(self):
|
if item >= len(self):
|
||||||
raise IndexError
|
raise IndexError
|
||||||
self.item = item
|
self.item = item
|
||||||
@ -70,9 +82,11 @@ def kron(_list):
|
|||||||
class Gate(State):
|
class Gate(State):
|
||||||
def on(self, state):
|
def on(self, state):
|
||||||
"""
|
"""
|
||||||
Applies
|
Applies a gate operation on a state.
|
||||||
:param state: another state (e.g. H(q1) or a list of states (e.g. for CNOT([q1, q2]))
|
If applying to a substate, use the index of the substate, e.g. `H.on(bell[0])` will apply the Hadamard gate
|
||||||
:return:
|
on the 0th qubit of the `bell` state.
|
||||||
|
:param state: another state (e.g. `H.on(q1)` or a list of states (e.g. for `CNOT.on([q1, q2])`)
|
||||||
|
:return: the state after the application of the Gate
|
||||||
"""
|
"""
|
||||||
this_state = self.matrix_state
|
this_state = self.matrix_state
|
||||||
if type(state) == list:
|
if type(state) == list:
|
||||||
@ -81,7 +95,8 @@ class Gate(State):
|
|||||||
other_state = state.matrix_state
|
other_state = state.matrix_state
|
||||||
if this_state.shape[1] != other_state.shape[0]:
|
if this_state.shape[1] != other_state.shape[0]:
|
||||||
# The two arrays are different sizes
|
# The two arrays are different sizes
|
||||||
# Use the Kronicker product of Identity with the state.item
|
# Use the Kronicker product of Identity with the state.item where
|
||||||
|
# state.item is the substate
|
||||||
larger_side = max(this_state.shape[1], this_state.shape[0])
|
larger_side = max(this_state.shape[1], this_state.shape[0])
|
||||||
_list = [this_state if i == state.item else _I for i in range(larger_side)]
|
_list = [this_state if i == state.item else _I for i in range(larger_side)]
|
||||||
this_state = kron(_list)
|
this_state = kron(_list)
|
||||||
|
Loading…
Reference in New Issue
Block a user