def test_phase_setter(self, pauli, phase): """Test phase setter""" pauli = Pauli(pauli) pauli.phase = phase _, coeff = _split_pauli_label(str(pauli)) value = _phase_from_label(coeff) self.assertEqual(value, phase)
def test_evolve_clifford2(self, gate, label): """Test evolve method for 2-qubit Clifford gates.""" op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(gate)) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target)
def test_equal(self, label1, label2): """Test __eq__ method""" pauli1 = Pauli(label1) pauli2 = Pauli(label2) target = np.all(pauli1.z == pauli2.z) and np.all( pauli1.x == pauli2.x) and pauli1.phase == pauli2.phase self.assertEqual(pauli1 == pauli2, target)
def test_expval_single_qubit(self): """Test expectation_value method of a single qubit""" num_qubits = 1 qc = QuantumCircuit(num_qubits) stab = StabilizerState(qc) pairs = [("Z", 1), ("X", 0), ("Y", 0), ("I", 1)] for label, target in pairs: with self.subTest(msg=f"<{label}>"): op = Pauli(label) expval = stab.expectation_value(op) self.assertEqual(expval, target) qc = QuantumCircuit(num_qubits) qc.x(0) stab = StabilizerState(qc) pairs = [("Z", -1), ("X", 0), ("Y", 0), ("I", 1)] for label, target in pairs: with self.subTest(msg=f"<{label}>"): op = Pauli(label) expval = stab.expectation_value(op) self.assertEqual(expval, target) qc = QuantumCircuit(num_qubits) qc.h(0) stab = StabilizerState(qc) pairs = [("Z", 0), ("X", 1), ("Y", 0), ("I", 1)] for label, target in pairs: with self.subTest(msg=f"<{label}>"): op = Pauli(label) expval = stab.expectation_value(op) self.assertEqual(expval, target)
def test_equiv(self, label1, label2): """Test equiv method""" pauli1 = Pauli(label1) pauli2 = Pauli(label2) target = np.all(pauli1.z == pauli2.z) and np.all( pauli1.x == pauli2.x) self.assertEqual(pauli1.equiv(pauli2), target)
def test_multiply(self, val): """Test multiply method.""" op = val * Pauli(([True, True], [False, False], 0)) phase = (-1j) ** op.phase self.assertEqual(phase, val) op = Pauli(([True, True], [False, False], 0)) * val phase = (-1j) ** op.phase self.assertEqual(phase, val)
def test_create_from_label(self): """Test creation from pauli label.""" label = 'IZXY' pauli = Pauli(label=label) self.assertEqual(pauli, self.ref_p) self.assertEqual(pauli.to_label(), self.ref_label) self.assertEqual(len(pauli), 4)
def test_compose(self, label1, label2): """Test compose method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.compose(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2) self.assertEqual(value, target)
def test_expand(self, label1, label2): """Test expand method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.expand(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.expand(op2) self.assertEqual(value, target)
def test_snapshot_pauli_type(self): """Test snapshot instruction has correct type.""" pauli_ops = [[[1, 'I'], [0.5, 'X'], [0.25, 'Y'], [-3, 'Z']], [[1j, 'I'], [0.5j, 'X'], [0.25j, 'Y'], [-3j, 'Z']], [[0.5j, Pauli('X')], [-0.5j, Pauli('Z')]]] for op in pauli_ops: # standard instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli') # Single shot instrs = [ SnapshotExpectationValue('snap', op, single_shot=True, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=True, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli_single_shot') # Variance with self.assertWarns(DeprecationWarning): instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=True).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=True) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli_with_variance')
def test_dot(self, label1, label2): """Test dot method.""" p1 = Pauli(label1) p2 = Pauli(label2) value = Operator(p1.dot(p2)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2) self.assertEqual(value, target) target = op1 @ op2 self.assertEqual(value, target)
def test_pauli_single(self): """Test pauli single.""" num_qubits = 5 pz = Pauli.pauli_single(num_qubits, 2, 'Z') self.assertTrue(pz.to_label(), 'IIIZI') py = Pauli.pauli_single(num_qubits, 4, 'Y') self.assertTrue(py.to_label(), 'IYIII') px = Pauli.pauli_single(num_qubits, 3, 'X') self.assertTrue(px.to_label(), 'IIXII')
def test_pauli_single(self): """Test pauli single.""" num_qubits = 5 pz = Pauli.pauli_single(num_qubits, 2, "Z") self.assertTrue(pz.to_label(), "IIIZI") py = Pauli.pauli_single(num_qubits, 4, "Y") self.assertTrue(py.to_label(), "IYIII") px = Pauli.pauli_single(num_qubits, 3, "X") self.assertTrue(px.to_label(), "IIXII")
def test_init_with_operators(self): """Test construction with mixture of operators.""" kraus = Kraus([np.sqrt(0.7) * np.eye(2), np.sqrt(0.3) * np.diag([1, -1])]) self.assertEqual(QuantumError(kraus).size, 1) self.assertEqual(QuantumError([(kraus, 0.7), (kraus, 0.3)]).size, 2) self.assertEqual(QuantumError(Pauli("X")).probabilities, [1.0]) self.assertEqual(QuantumError([(Pauli("I"), 0.7), (Pauli("Z"), 0.3)]).size, 2) self.assertEqual(QuantumError([(Pauli("I"), 0.7), (kraus, 0.3)]).size, 2)
def test_compose_qargs(self, label2): """Test compose method with qargs.""" label1 = '-XYZ' p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.compose(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.compose(op2, qargs=qargs) self.assertEqual(value, target)
def test_dot_qargs(self, label2): """Test dot method with qargs.""" label1 = '-iXYZ' p1 = Pauli(label1) p2 = Pauli(label2) qargs = [0] value = Operator(p1.dot(p2, qargs=qargs)) op1 = operator_from_label(label1) op2 = operator_from_label(label2) target = op1.dot(op2, qargs=qargs) self.assertEqual(value, target)
def test_evolve_clifford2(self, gate, label): """Test evolve method for 2-qubit Clifford gates.""" op = Operator(gate) pauli = Pauli(label) value = Operator(pauli.evolve(gate)) value_h = Operator(pauli.evolve(gate, frame="h")) value_s = Operator(pauli.evolve(gate, frame="s")) value_inv = Operator(pauli.evolve(gate.inverse())) target = op.adjoint().dot(pauli).dot(op) self.assertEqual(value, target) self.assertEqual(value, value_h) self.assertEqual(value_inv, value_s)
def stabilizes_statevector(stabilizer, statevector): """Return True if two stabilizer states are equal.""" # Get stabilizer and destabilizers and convert to sets for stab in stabilizer: if stab[0] == '-': pauli_mat = -1 * Pauli.from_label(stab[1:]).to_matrix() else: pauli_mat = Pauli.from_label(stab).to_matrix() val = statevector.conj().dot(pauli_mat.dot(statevector)) if not np.isclose(val, 1): return False return True
def map(self, second_q_op: VibrationalOp) -> PauliSumOp: nmodes = sum(second_q_op.num_modals) pauli_table = [] for i in range(nmodes): a_z = np.asarray([0] * i + [0] + [0] * (nmodes - i - 1), dtype=bool) a_x = np.asarray([0] * i + [1] + [0] * (nmodes - i - 1), dtype=bool) b_z = np.asarray([0] * i + [1] + [0] * (nmodes - i - 1), dtype=bool) b_x = np.asarray([0] * i + [1] + [0] * (nmodes - i - 1), dtype=bool) pauli_table.append((Pauli((a_z, a_x)), Pauli((b_z, b_x)))) return QubitMapper.mode_based_mapping(second_q_op, pauli_table)
def test_barrier_delay_sim(self): """Test barrier and delay instructions can be simulated""" target_circ = QuantumCircuit(2) target_circ.x(0) target_circ.y(1) target = Pauli(target_circ) circ = QuantumCircuit(2) circ.x(0) circ.delay(100, 0) circ.barrier([0, 1]) circ.y(1) value = Pauli(circ) self.assertEqual(value, target)
def synthesize(self, evolution): # get operators and time to evolve operators = evolution.operator time = evolution.time # construct the evolution circuit evolution_circuit = QuantumCircuit(operators[0].num_qubits) first_barrier = False if not isinstance(operators, list): pauli_list = [(Pauli(op), np.real(coeff)) for op, coeff in operators.to_list()] else: pauli_list = [(op, 1) for op in operators] # if we only evolve a single Pauli we don't need to additionally wrap it wrap = not (len(pauli_list) == 1 and self.reps == 1) for _ in range(self.reps): for op, coeff in pauli_list: # add barriers if first_barrier: if self.insert_barriers: evolution_circuit.barrier() else: first_barrier = True evolution_circuit.compose(self.atomic_evolution( op, coeff * time / self.reps), wrap=wrap, inplace=True) return evolution_circuit
def test_pershot_kwarg(self): """Test pershot kwarg""" key = 'test_key' instr = SaveExpectationValue(key, Pauli('X'), pershot=True) self.assertEqual(instr.name, 'save_expval') self.assertEqual(instr._key, key) self.assertEqual(instr._subtype, 'list')
def test_unnorm_kwarg(self): """Test unnormalized kwarg""" key = 'test_key' instr = SaveExpectationValue(key, Pauli('X'), unnormalized=True) self.assertEqual(instr.name, 'save_expval') self.assertEqual(instr._key, key) self.assertEqual(instr._subtype, 'accum')
def test_cond_kwarg(self): """Test conditional kwarg""" key = 'test_key' instr = SaveExpectationValue(key, Pauli('X'), conditional=True) self.assertEqual(instr.name, 'save_expval') self.assertEqual(instr._key, key) self.assertEqual(instr._subtype, 'c_average')
def test_default_kwarg(self): """Test default kwargs""" key = 'test_key' instr = SaveExpectationValue(key, Pauli('X')) self.assertEqual(instr.name, 'save_expval') self.assertEqual(instr._key, key) self.assertEqual(instr._subtype, 'average')
def test_can_append_to_quantum_circuit(self): """Test that we can add various objects with Operation interface to a Quantum Circuit.""" qc = QuantumCircuit(6, 1) qc.append(XGate(), [2]) qc.append(Barrier(3), [1, 2, 4]) qc.append(CXGate(), [0, 1]) qc.append(Measure(), [1], [0]) qc.append(Reset(), [0]) qc.cx(3, 4) qc.append(Gate("some_gate", 3, []), [1, 2, 3]) qc.append(Initialize([0.5, 0.5, 0.5, 0.5]), [4, 5]) qc.append(Isometry(np.eye(4, 4), 0, 0), [3, 4]) qc.append(Pauli("II"), [0, 1]) # Appending Clifford circ1 = QuantumCircuit(2) circ1.h(1) circ1.cx(0, 1) qc.append(Clifford(circ1), [0, 1]) # Appending CNOTDihedral circ2 = QuantumCircuit(2) circ2.t(0) circ2.x(0) circ2.t(1) qc.append(CNOTDihedral(circ2), [2, 3]) # If we got to here, we have successfully appended everything to qc self.assertIsInstance(qc, QuantumCircuit)
def test_getitem(self, label, qubits): """Test __getitem__""" pauli = Pauli(label) value = str(pauli[qubits]) val_array = np.array(list(reversed(label)))[qubits] target = ''.join(reversed(val_array.tolist())) self.assertEqual(value, target, msg='indices = {}'.format(qubits))
def test_mul(self): """Test multiplication.""" p1 = self.ref_p p2 = Pauli.from_label('ZXXI') p3 = p1 * p2 self.assertEqual(len(p3), 4) self.assertEqual(p3[:].to_label(), 'ZYIY')
def __array__(self, dtype=None): """Return a Numpy.array for the pauli gate. i.e. tensor product of the paulis""" # pylint: disable=cyclic-import from qiskit.quantum_info.operators import Pauli return Pauli(self.params[0]).__array__(dtype=dtype)
def _pauli_id(n_qubits: int, coeff=None) -> SparsePauliOp: """Return the identity for `SparsePauliOp` on `n_qubits` qubits.""" if coeff is None: coeff = complex(1.0) return SparsePauliOp( Pauli((np.zeros(n_qubits, dtype=bool), np.zeros(n_qubits, dtype=bool))), [coeff])