def test_1_qubit_identities(self): """Tests identities for 1-qubit gates""" # T*X*T = X circ1 = QuantumCircuit(1) circ1.t(0) circ1.x(0) circ1.t(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(XGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*T*X = Tdg circ1 = QuantumCircuit(1) circ1.x(0) circ1.t(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(TdgGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*Tdg*X = T circ1 = QuantumCircuit(1) circ1.x(0) circ1.tdg(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(TGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*S*X = Sdg circ1 = QuantumCircuit(1) circ1.x(0) circ1.s(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(SdgGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # X*Sdg*X = S circ1 = QuantumCircuit(1) circ1.x(0) circ1.sdg(0) circ1.x(0) elem1 = CNOTDihedral(circ1) elem = CNOTDihedral(SGate()) self.assertEqual(elem1, elem, "Error: 1-qubit identity does not hold") # T*X*Tdg = S*X circ1 = QuantumCircuit(1) circ1.t(0) circ1.x(0) circ1.tdg(0) circ2 = QuantumCircuit(1) circ2.s(0) circ2.x(0) elem1 = CNOTDihedral(circ1) elem2 = CNOTDihedral(circ2) self.assertEqual(elem1, elem2, "Error: 1-qubit identity does not hold")
def test_inverse_with_different_names(self): """Test that inverse gates that have different names.""" qc = QuantumCircuit(2, 2) qc.t(0) qc.tdg(0) pass_ = InverseCancellation([(TGate(), TdgGate())]) pm = PassManager(pass_) new_circ = pm.run(qc) gates_after = new_circ.count_ops() self.assertNotIn("t", gates_after) self.assertNotIn("tdg", gates_after)
def random_cnotdihedral_circuit(num_qubits, num_gates, gates='all', seed=None): """Generate a pseudo random CNOTDihedral circuit.""" if gates == 'all': if num_qubits == 1: gates = ['i', 'x', 'y', 'z', 't', 'tdg', 's', 'sdg'] else: gates = [ 'i', 'x', 'y', 'z', 't', 'tdg', 's', 'sdg', 'cx', 'cz', 'swap' ] instructions = { 'i': (IGate(), 1), 'x': (XGate(), 1), 'y': (YGate(), 1), 'z': (ZGate(), 1), 's': (SGate(), 1), 'sdg': (SdgGate(), 1), 't': (TGate(), 1), 'tdg': (TdgGate(), 1), 'cx': (CXGate(), 2), 'cz': (CZGate(), 2), 'swap': (SwapGate(), 2) } if isinstance(seed, np.random.Generator): rng = seed else: rng = np.random.default_rng(seed) samples = rng.choice(gates, num_gates) circ = QuantumCircuit(num_qubits) for name in samples: gate, nqargs = instructions[name] qargs = rng.choice(range(num_qubits), nqargs, replace=False).tolist() circ.append(gate, qargs) return circ
def random_cnotdihedral_circuit(num_qubits, num_gates, gates="all", seed=None): """Generate a pseudo random CNOTDihedral circuit.""" if gates == "all": if num_qubits == 1: gates = ["i", "x", "y", "z", "t", "tdg", "s", "sdg"] else: gates = [ "i", "x", "y", "z", "t", "tdg", "s", "sdg", "cx", "cz", "swap" ] instructions = { "i": (IGate(), 1), "x": (XGate(), 1), "y": (YGate(), 1), "z": (ZGate(), 1), "s": (SGate(), 1), "sdg": (SdgGate(), 1), "t": (TGate(), 1), "tdg": (TdgGate(), 1), "cx": (CXGate(), 2), "cz": (CZGate(), 2), "swap": (SwapGate(), 2), } if isinstance(seed, np.random.Generator): rng = seed else: rng = np.random.default_rng(seed) samples = rng.choice(gates, num_gates) circ = QuantumCircuit(num_qubits) for name in samples: gate, nqargs = instructions[name] qargs = rng.choice(range(num_qubits), nqargs, replace=False).tolist() circ.append(gate, qargs) return circ
from qiskit.circuit.library import HGate, XGate, YGate, ZGate, CXGate, CYGate, CZGate, CHGate from qiskit.circuit.library import SwapGate, IGate, SGate, TGate, TdgGate, SdgGate, RYGate from qiskit import QuantumCircuit, Aer, execute, QuantumRegister, ClassicalRegister import numpy as np SINGLE_GATE_DICT = { 'I' : IGate(), 'H' : HGate(), 'X' : XGate(), 'Y' : YGate(), 'Z' : ZGate(), 'S' : SGate(), 'T' : TGate(), 'T_dg' : TdgGate(), 'S_dg' : SdgGate(), 'Ry' : RYGate(np.pi / 4) } CONTROLLED_GATE_DICT = { 'CX0' : CXGate(), 'CX1' : CXGate(), 'CY0' : CYGate(), 'CY1' : CYGate(), 'CZ0' : CZGate(), 'CZ1' : CYGate(), 'CH0' : CHGate(), 'CH1' : CHGate() } def _state_to_gates(state):