Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test #52

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QPandaLiteCpp/Pybinder/QPandaLitePy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ PYBIND11_MODULE(QPandaLitePy, m)
.def("insert_error", &qpandalite::NoisySimulator::insert_error)
.def("get_measure_no_readout_error", &qpandalite::NoisySimulator::get_measure_no_readout_error)
.def("get_measure", &qpandalite::NoisySimulator::get_measure)
.def("measure_shots", &qpandalite::NoisySimulator::measure_shots)

.def("id", &qpandalite::NoisySimulator::id, py::arg("qn"), py::arg("is_dagger") = false)
.def("hadamard", &qpandalite::NoisySimulator::hadamard, py::arg("qn"), py::arg("dagger") = false)
Expand Down
4 changes: 2 additions & 2 deletions qpandalite/simulator/QPandaLitePy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ class NoisySimulator:
...


def measure_shots(self, shots: int) -> Dict[int, int]:
def measure_shots(self, measure_qubits: List[int], shots: int) -> Dict[int, int]:
"""
Simulate a quantum measurement multiple times and tally the results.

Expand All @@ -675,7 +675,7 @@ class NoisySimulator_GateDependent(NoisySimulator):

GateError1q_t = Dict[Tuple[SupportOperationType, int], Dict[NoiseType, float]]
GateError2q_t = Dict[Tuple[SupportOperationType, int], Dict[NoiseType, float]]
class NoisySimulator_GateErrorSpecific(NoisySimulator):
class NoisySimulator_GateSpecificError(NoisySimulator):

gate_error1q: Dict[str, Dict[str, float]]

Expand Down
73 changes: 73 additions & 0 deletions qpandalite/test/common_gates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import numpy as np

# Some commonly-used quantum gates

# Identity
I_Gate = np.array([[1, 0],
[0, 1]], dtype=complex)

# Pauli-X (NOT)
X_Gate = np.array([[0, 1],
[1, 0]], dtype=complex)

# Pauli-Y
Y_Gate = np.array([[0, -1j],
[1j, 0]], dtype=complex)

# Pauli-Z (Phase)
Z_Gate = np.array([[1, 0],
[0, -1]], dtype=complex)

# Hadamard
H_Gate = (1/np.sqrt(2)) * np.array([[1, 1],
[1, -1]], dtype=complex)

# Phase (S)
S_Gate = np.array([[1, 0],
[0, 1j]], dtype=complex)

# Pi/8 (T)
T_Gate = np.array([[1, 0],
[0, np.exp(1j * np.pi / 4)]], dtype=complex)

# Controlled NOT (CNOT)
# Note: This is a 2-qubit gate represented in a 4x4 matrix
CNOT_Gate = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]], dtype=complex)

# Controlled Z-Gate (CZ)
# Note: This is a 2-qubit gate represented in a 4x4 matrix
CZ_Gate = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, -1]], dtype=complex)
# Controlled NOT (CNOT)
# Note: This is a 2-qubit gate represented in a 4x4 matrix
SWAP_Gate = np.array([[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]], dtype=complex)

# The Controlled-SWAP (Fredkin) gate matrix
CSWAP_Gate = np.array([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1]], dtype=complex)

def RX_Gate(theta):
return np.array([[np.cos(theta/2), -1j*np.sin(theta/2)],
[-1j*np.sin(theta/2), np.cos(theta/2)]], dtype=complex)

def RY_Gate(theta):
return np.array([[np.cos(theta/2), -np.sin(theta/2)],
[np.sin(theta/2), np.cos(theta/2)]], dtype=complex)

def RZ_Gate(theta):
return np.array([[np.exp(-1j*theta/2), 0],
[0, np.exp(1j*theta/2)]], dtype=complex)
19 changes: 14 additions & 5 deletions qpandalite/test/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
from qpandalite.qasm_origin import OpenQASM2_Parser
from qpandalite.circuit_builder import Circuit

from qpandalite.test import common_gates

def iswap_test():
sim = qsim.Simulator()
sim.init_n_qubit(3)
sim.sx(1)
sim.xy(0, 1)

print(sim.state)
sim.init_n_qubit(2)
# Create the vector
vector = np.zeros(2**2)
vector[0] = 1
sim.hadamard(0)
sim.hadamard(1)
# sim.xy(0, 1, np.pi/8)

result = np.kron(common_gates.H_Gate, common_gates.H_Gate) @ vector
if not np.allclose(result, np.array(sim.state)):
print(result, np.array(sim.state))
raise AssertionError("iswap_test failed")


def run_test_general():
pass
Expand Down
6 changes: 3 additions & 3 deletions qpandalite/test/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from qpandalite.simulator import (Simulator,
NoisySimulator,
NoisySimulator_GateDependent,
NoisySimulator_GateErrorSpecific)
NoisySimulator_GateSpecificError)
from qpandalite.qasm_origin import OpenQASM2_Parser
from qpandalite.circuit_builder import Circuit
from qpandalite.simulator import seed
Expand Down Expand Up @@ -36,5 +36,5 @@ def test_noisy_simulator():

# Create an instance of the NoisySimulator
simulator = NoisySimulator(2,noise_description, measurement_error)

measurement_results = simulator.measure_shots(measure_qubit=measure_qubits, shots=shots)
measurement_results = simulator.measure_shots(measure_qubits, shots)